Is there any way to do HTTP PUT request in Python?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Yes, Python can make HTTP PUT requests easily. The most common tool is the requests library, which gives you a straightforward requests.put(...) API for sending JSON, form data, headers, authentication, and timeouts.
The bigger question is usually not whether Python supports PUT, but how to send the body correctly and how to handle the response safely. That is where most real mistakes happen.
Use requests.put
The simplest PUT request looks like this:
Passing json=payload tells requests to serialize the payload as JSON and send the appropriate Content-Type header automatically. For modern APIs, this is often the cleanest form.
json= Versus data=
One of the most important details is the difference between json= and data=.
- '
json=serializes Python data to JSON.' - '
data=sends raw bytes or form-encoded data depending on what you pass.'
For raw JSON text:
This works, but json= is shorter and less error-prone when the server expects JSON.
Add Headers And Authentication
Many PUT endpoints require headers or authentication tokens. requests lets you provide them explicitly:
raise_for_status() is useful because it turns HTTP error codes into exceptions, which makes failure handling more consistent in scripts and applications.
Send Raw Content When Needed
Some APIs expect a full text or binary replacement rather than a JSON object. In those cases, send the raw body directly:
That is still a PUT request. The HTTP method and the body format are separate concerns.
Standard Library Option
If you do not want an external dependency, Python's standard library can also send a PUT request with urllib.request, though the code is more verbose.
For most application code, requests remains easier to read and maintain.
Understand What PUT Semantically Means
PUT is commonly used to create or replace the representation at a specific URL, and it is defined to be idempotent. That means repeating the same PUT request should have the same effect as sending it once, assuming the server implements the endpoint in the usual way.
That does not mean every API follows the spirit perfectly, but it is the design expectation. If the operation is "append a new item to a collection," POST is often more appropriate. If the operation is "replace or update the resource at this known URL," PUT is often the better fit.
Common Pitfalls
One common mistake is using data= when the server expects JSON and then forgetting the Content-Type header. Another is omitting timeouts, which can leave a script hanging indefinitely on a slow network. Developers also sometimes assume a successful TCP request means the update worked, but many APIs return validation errors with normal HTTP responses, so you still need to inspect status codes and response bodies. Finally, do not confuse PUT semantics with partial updates. Some APIs use PATCH for partial modification and reserve PUT for full replacement.
Summary
- Yes, Python can send HTTP
PUTrequests easily, most commonly withrequests.put(...). - Use
json=when the server expects JSON anddata=for raw or form-style bodies. - Add timeouts, headers, and
raise_for_status()for safer code. - The standard library can also send
PUT, butrequestsis usually more ergonomic. - Be clear about API semantics, especially the difference between
PUT,POST, andPATCH.
Related reading
- Is there anyway to get the external ports of the kubernetes cluster
- Is there option to redirect http traffic to https in aws network load balancer
- Issue with ArrayList Serde in Kafka Streams API
- issue with Ingress and OAuth2 Proxy error 500
- Is there any way to list queues in rabbitmq via pika?
- Is there any way to show the dependency trees for pip packages?
- Issues with stability with Kubernetes cluster before adding networking
- istio-proxy closing long running TCP connection after 1 hour

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.