Sending PUT Request [Python Code]

To send a PUT request with Python, use the HTTP PUT method and provide the data in the body of the PUT message. The HTTP PUT request method creates a new resource or replaces an existing resource on the server. The Content-Type request header specifies the data type in the body of the PUT request message, and the Content-Length request header specifies the data size in the PUT request body. In contrast to a PATCH request, which partially replaces a resource on the server and can contain only partial data, a PUT request completely overwrites the resource and must have a complete copy of the data. In this Python PUT Request Example, we send JSON to the ReqBin echo URL. Click Send to execute Python PUT request online and see the results. The Python code was automatically generated for the PUT Request example.
Sending PUT Request [Python Code] Send
PUT /echo/put/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 80

{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}
Updated: Viewed: 71479 times
Python code for PUT Request example

Python code for PUT Request Example

This Python code snippet was generated automatically for the PUT Request example.
<< Back to the PUT Request example

What is HTTP?

HTTP (Hypertext Transfer Protocol) is a communication protocol between two computers and is widely used to transfer data between an HTTP client (browser or mobile app) and the server. HTTP is built around messages called request and response. Each HTTP message consists of a request line (or a status line for a response), HTTP headers, and a message body.

What is the PUT request method used for?

The HTTP PUT method creates or replaces the current representation of the resource on the server with new data. The PUT method is typically used to update data to the server. If the resource does not exist at the specified URI, the server should create it and return the 201 (Created) status code. If the resource is updated, the server should return a 200 (OK) or 204 (No Content) status code. The Content-Type header indicates the data type in the body of the PUT message. If the server cannot recognize the provided data or the data is invalid in the current context, the server should return a 501 (Not Implemented) status code.

Basic PUT Request Example

An example of sending the PUT request to the ReqBin echo URL:

PUT Request Example
PUT /echo/put HTTP/1.1
Host: reqbin.com
Content-Length: 1000

[put request data]

What is the difference between the PUT and POST request methods?

The main difference between the PUT and POST methods is the PUT method is used when you need to update a resource, and the POST method is used when you need to add a resource. The PUT method is idempotent, which means that the client can make multiple PUT requests for the same URI without creating duplicate entries on the server, and the POST requests will change the server state after each request (may create duplicate entries). Browsers and proxy servers should not cache PUT requests.

How to send JSON data using PUT method?

To send JSON to the server, you must include the JSON data in the body of the PUT message and provide a valid Content-Type and Content-Length headers. In the example below, we make a PUT request with JSON data to the ReqBin echo URL.

PUT Request Example
PUT /echo/put/json HTTP/1.1
Host: reqbin.com
Content-Type: application/json
Content-Length: 80

{
  "Id": 12345,
  "Customer": "John Smith",
  "Quantity": 1,
  "Price": 10.00
}

Response to our PUT request:

PUT Request Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

{"success":"true"}

See also

Generate code snippets for Python and other programming languages

Convert your PUT Request request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.