POST Method

About1

The POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header. Also known as a POST request .

Difference between POST and PUT

The difference between PUT and POST is that PUT is idempotent . On the other hand, successive POST requests may have additional effects, such as creating the same order several times.

Sending data using POST

HTML forms typically send data using POST and this usually results in a change on the server. For HTML forms the format/ encoding of the boyd content is determined by tghe enctype attribute of the <form> element or the formenctype attribute of the <input> or <button> elements.

POST encoding

The encoding may be one of the following:

  • application/x-www-form-urlencoded: the keys and values are encoded in key-value tuples separated by an ampersand (&), with an equals symbol (=) between the key and the value (e.g., first-name=Joe&last-name=Doe). Non-alphanumeric characters in both keys and values are percent-encoded: this is the reason why this type is not suitable to use with binary data and you should use multipart/form-data for this purpose instead.
  • multipart/form-data: each value is sent as a block of data (“body part”), with a user agent-defined delimeter (e.g., boundary="delimeter133"). separting each parth. The keys are described in the Content-Disposition header of each part or block of data.
  • text/plain

POST for non-HTML-related requests

The body can be any type if POST is made using any other calls that are not HTML.

POST functionality

POST is designed to allow a uniform method to cover the following functions:

  • Annotation of existing resources
  • Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles
  • Adding a new user through a signup form
  • Providing a block of data, e.g. as the result of submitting a form, to a data-handling process
  • Extending a database through an append operation

Characteristics

Request has bodySuccessful response has bodySafeIdempotentCacheableAllowed in HTML forms
YesYesNoNoOnly if freshness information is includedYes

Syntax

POST <request-target>["?"<query>] HTTP/1.1
  • <request-target>: identifies the target resource of the request when combined with the information provided in the

Host header. This is an absolute path (e.g. /path/to/file.html) in requests to an origin server, and an absolute URL in requests to proxies (e.g. http://www.example.com/path/to/file.html)

  • <query> (Optional) - an optional query component preceded by a question-mark ?. Often used to carry identifying

information in the form of key=value pairs.

Examples

URL-encoded form submission

A form using application/x-www-form-urlencoded content encoding (the default) sends a request where the body contains the form data in key=value paris, with each pair separated by an & symbol, as shown below:

POST /test HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 27

field1=value1&field2=value2

Multipart form submission

The multipart/form-data encoding is used when a form includes files or a lot of data. This request body delineates each part of the form using a boundary string. An example of a request in this format:

POST /test HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary="delimeter1234"

--delimeter1234
Content-Disposition: form-data; name="field1"

value1
--delimeter1234
Content-Disposition: form-data; name="field2"; filename="example.txt"

value2
--delimeter1234--

The Content-Disposition header indicates how the form data should be processed, specifying the field name and filename, if appropriate.

Anki

References


  1. MDN. “POST method”. Available at: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods/POST . (Accessed: [2025-05-06 Tue 21:08]). ↩︎

Random Posts