How do I send HTTP PUT request? [PHP Code]

The HTTP PUT request method creates a new resource or replaces an existing resource on the server. The Content-Type request header indicates the media type of the PUT request body, and the Content-Length request header indicates the data size in the PUT request message. In this PUT Request Example, we send JSON to the ReqBin echo URL. Click Send to execute the PUT request online and see the results. The PHP code was automatically generated for the PUT Request example.
How do I send HTTP PUT request? [PHP 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: 28709 times
PHP code for PUT Request example

PHP code for PUT Request Example

This PHP 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
}

See also

Generate code snippets for PHP and other programming languages

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