Python
HTTP PUT
HTTP requests
Python programming
Python HTTP library

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.

Practice system design

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:

python
1import requests
2
3url = "https://httpbin.org/put"
4payload = {"name": "Ada", "role": "engineer"}
5
6response = requests.put(url, json=payload, timeout=10)
7print(response.status_code)
8print(response.json())

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:

python
1import json
2import requests
3
4payload = {"enabled": True}
5response = requests.put(
6    "https://httpbin.org/put",
7    data=json.dumps(payload),
8    headers={"Content-Type": "application/json"},
9    timeout=10,
10)

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:

python
1import requests
2
3headers = {
4    "Authorization": "Bearer MY_TOKEN",
5    "Accept": "application/json",
6}
7
8response = requests.put(
9    "https://api.example.com/users/42",
10    json={"name": "Ada"},
11    headers=headers,
12    timeout=10,
13)
14
15response.raise_for_status()
16print(response.json())

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:

python
1import requests
2
3response = requests.put(
4    "https://httpbin.org/put",
5    data="plain text body",
6    headers={"Content-Type": "text/plain"},
7    timeout=10,
8)
9
10print(response.status_code)

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.

python
1import json
2import urllib.request
3
4data = json.dumps({"name": "Ada"}).encode("utf-8")
5req = urllib.request.Request(
6    "https://httpbin.org/put",
7    data=data,
8    method="PUT",
9    headers={"Content-Type": "application/json"},
10)
11
12with urllib.request.urlopen(req, timeout=10) as resp:
13    print(resp.status)
14    print(resp.read().decode("utf-8"))

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 PUT requests easily, most commonly with requests.put(...).
  • Use json= when the server expects JSON and data= for raw or form-style bodies.
  • Add timeouts, headers, and raise_for_status() for safer code.
  • The standard library can also send PUT, but requests is usually more ergonomic.
  • Be clear about API semantics, especially the difference between PUT, POST, and PATCH.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.