ArticleslgStudy

science

PATCH (HTTP)

PATCH (HTTP) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand PATCH (HTTP) rather than just read about it. In short: In computing, the PATCH method is a request method in HTTP for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP Uniform Resource Identifier (URI).

PATCH (HTTP) — main illustration
PATCH (HTTP) — illustration

Key takeaways

  • PATCH (HTTP) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect PATCH (HTTP) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of PATCH (HTTP) from memory before moving on to harder problems.

Reference excerpt

In computing, the PATCH method is a request method in HTTP for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP Uniform Resource Identifier (URI). The list of changes are supplied in the form of a PATCH document. If the requested resource does not exist then the server may create the resource depending on the PATCH document media type and permissions. The changes described in the PATCH document must be semantically well defined but can have a different media type than the resource being patched. Languages such as XML or JSON can be used in describing the changes in the PATCH document.

History of PATCH As per the semantics defined in the HTTP protocol, the GET, PUT, and POST methods need to use a full representation of the resource. The PUT method which can be used for resource creation or replacement is idempotent and can be used only for full updates. The edit forms used in conventional Ruby on Rails application need to create new resources by applying partial updates to a parent resource. Due to this requirement, the PATCH method was added to the HTTP protocol in 2010.

PUT vs PATCH vs POST HTTP is the foundation of data communication for the World Wide Web. It is a request-response protocol which helps users communicate with the server to perform CRUD operations. HTTP defines a number of request methods such as PUT, POST and PATCH to create or update resources. The main difference between the PUT and PATCH method is that the PUT method supplies a modified version of the requested resource which replaces the original version of the resource, whereas the PATCH method supplies a set of instructions to modify the resource. If the PATCH document is larger than the size of the new version of the resource sent by the PUT method then the PUT method is preferred. The POST method can be used for sending partial updates to a resource. The main difference between the POST and PATCH methods is that the POST method can be used only when it is written to support the applications or the applications support its semantics whereas the PATCH method can be used in a generic way and does not require application support. If the outcome of using the PATCH method is not known then the POST method is preferred.

Patching resources The PATCH method is atomic. Either all the changes specified by the PATCH method are applied or none of the changes are applied by the server. There are many ways of checking whether a patch was applied successfully. For example, the 'diff' utility can be applied to the older version and newer version of a file to find the differences between them. A cached PATCH response is considered stale. It can only be used for the GET and HEAD requests that may follow the PATCH request. The entity headers in the PATCH document are only applicable to the PATCH document and cannot be applied to the requested resource. There is no standard format for the PATCH document and it is different for different types of resources. The server has to check whether the PATCH document received is appropriate for the requested resource. A JSON Patch document would look like

"op" represents the operation performed on the resource. "path" represents the resource being modified. "value" represents the amount being added to the existing resource. Before applying the changes in the PATCH document, the server has to check whether the PATCH document received is appropriate for the requested resource. If the PATCH request succeeds then it returns a 204 response. A XML PATCH document would look like

The element <user> is located using the 'email' attribute. A new attribute 'address' with the value "ABC Road" is added to the <user> element.

Example A simple PATCH request example

Successful PATCH response to existing text file:

HTTP/1.1 204 No Content Content-Location: /example.txt ETag: "dd541480"

The response 204 means that the request was processed successfully.

Trade-offs between PUT and PATCH Using the PUT method consumes more bandwidth as compared to the PATCH method when only a few changes need to be applied to a resource. But when the PATCH method is used, it usually involves fetching the resource from the server, comparing the original and new files, creating and sending a diff file. On the server side, the server has to read the diff file and make the modifications. This involves a lot of overhead compared to the PUT method. On the other hand, the PUT method requires a GET to be performed before the PUT and it is difficult to ensure that the resource is not modified between the GET and PUT requests.

Caution The PATCH method is not "safe" in the sense of RFC 2616: it may modify resources, not necessarily limited to those mentioned in the URI. The PATCH method is not idempotent. It can be made idempotent by using a conditional request. When a client makes a conditional request to a resource, the request succeeds only if the resource has not been updated since the client last accessed that resource. This also helps in preventing corruption of the resource since some updates to a resource can only be performed starting from a certain base point.

Error handling A PATCH request can fail if any of the following errors occur:

Malformed patch document The server returns a 400 (Bad request) response if the PATCH document is not formatted as required.

Unsupported patch document The server returns a 415 (Unsupported Media Type) response with an Accept-Patch response header containing supported media types when the client sends a patch document in a format not implemented by the server. This informs the client that the PATCH document sent by the client cannot be applied to the requested resource.

Unprocessable request The server returns a 422 (Unprocessable Entity) response when the server understands the PATCH document but is unable to modify the requested resource either because it causes the resource to become invalid or it results in some other error state.

Resource not found The server returns a 404 (Not Found) response when the PATCH document cannot be applied to a non-existent resource.

Conflicting state The server returns a 409 (Conflict) response when the server cannot apply a patch for the current state of the resource.

… excerpt ends here. Continue reading the full article.

Illustrations

PATCH (HTTP) illustration

Worked examples

Example 1 — a first encounter with PATCH (HTTP)

Start with the simplest possible case. Write down what PATCH (HTTP) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to PATCH (HTTP) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about PATCH (HTTP) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of PATCH (HTTP)

In research
PATCH (HTTP) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses PATCH (HTTP) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
PATCH (HTTP) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Hypertext Transfer Protocol, so understanding it makes those chapters shorter.
In everyday life
Look for PATCH (HTTP) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “PATCH (HTTP)” →

How to study PATCH (HTTP) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what PATCH (HTTP) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain PATCH (HTTP) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is PATCH (HTTP) in simple terms?

In computing, the PATCH method is a request method in HTTP for making partial changes to an existing resource. The PATCH method provides an entity containing a list of changes to be applied to the resource requested using the HTTP Uniform Resource Identifier (URI).

Why does PATCH (HTTP) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study PATCH (HTTP)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on PATCH (HTTP).

Tags

  • Hypertext Transfer Protocol

Keep exploring