Sending PATCH Request [JavaScript/AJAX Code]

To send a PATCH request to the server using JavaScript/AJAX, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The PATCH request method is used to modify a resource on the server partially. The PATCH request data must contain instructions for partially modifying (patching) the data on the server. The Content-Type request header must indicate the data type in the body. In this JavaScript/AJAX PATCH Request example, we send JSON to the ReqBin echo endpoint to update the data on the server. Click Send to run the JavaScript/AJAX PATCH Request example online and see the results. The JavaScript/AJAX code was automatically generated for the PATCH Request example.
Sending PATCH Request [JavaScript/AJAX Code] Send
PATCH /echo/patch/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 61

{
  "User": {
    "Id": 12345,
    "Name": "John Smith"
  }
}
Updated: Viewed: 72455 times
JavaScript/AJAX code for PATCH Request example

JavaScript/AJAX code for PATCH Request Example

This JavaScript/AJAX code snippet was generated automatically for the PATCH Request example.
<< Back to the PATCH Request example

What is HTTP?

HTTP is an application layer data transfer protocol widely used to transfer data between HTTP clients (browsers, mobile apps, etc.) and servers. HTTP defines various request methods that indicate the desired action (add, update, delete, etc.) for a given resource. Although request method names can be nouns, these methods are sometimes referred to as HTTP verbs. Each request method implements its own semantics and performs a specific action, but all methods can be combined into groups with common properties. For example, a group can determine if the methods in the group are safe or not.

What is the HTTP PATCH request method used for?

The HTTP PATCH request method is used to modify an existing resource on the server partially. The PATCH request method may or may not be idempotent, unlike HTTP PUT, which is always idempotent. An operation is considered idempotent if sending an identical PATCH request multiple times produces the same result as a single operation, does not affect the further state of the server, and does not cause additional side effects.

Can I send data with a PATCH request?

You can send data to the server in the body of an HTTP PATCH message the same way as the POST request. The type and size of data are not limited. The Content-Type request header must indicate the correct data type in the body of the PATCH message.

PATCH Request Example

The following is an example of sending a PATCH request to a ReqBin echo API endpoint:

PATCH Request Example
PATCH /echo/patch HTTP/1.1
Host: reqbin.com
Content-Length: 80
      
[patch request data]

What is the difference between the PATCH and PUT HTTP request methods?

The main difference between HTTP PATCH and PUT requests is how the server handles the resource specified in the Request-URI. When a PUT request is made, the request's data is treated as a modified version of the object stored on the origin server, and the client requests a full replacement. However, PATCH's request's data is treated as a partial object and contains instructions describing how PATCH should partially modify the resource stored on the origin server. The second difference is idempotency. HTTP PUT is idempotent because it always gives the same results after making multiple requests. On the other hand, the HTTP PATCH is generally considered non-idempotent. However, it can be idempotent depending on how the PATCH request will modify the object.

See also

Generate code snippets for JavaScript/AJAX and other programming languages

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