Four ways to submit data for POST requests

Content-Type attribute value

The HTTP request methods specified in the HTTP/1.1 protocol are OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT. POST is generally used to submit data to the server.
The HTTP protocol is an application layer specification based on the TCP/IP protocol, which is transmitted by ASCII code. The specification divides HTTP requests into three parts: the status line, the request header, and the message body. Similar to the following:

<method> <url> <version>
<headers>
<entity-body>

The protocol specifies that data submitted by POST must be placed in the entity-body, but it does not specify how the data must be encoded. In fact, the format of the message body is entirely up to the developer as long as the last HTTP request sent satisfies the above format. However, it makes sense for the server to successfully parse the data before it is sent out. Common server-side languages such as php, python, Java,.NET, and their framework s all have built-in capabilities to automatically parse common data formats. The service side usually knows how the message body in the request is encoded based on the Content-Type field in the headers, and then parses the body. That is, Content-Type specifies how the message body is encoded. Therefore, POST submits the data schema directly related to the Content-Type and the message body.

Content-Type attribute value

1,application/x-www-form-urlencoded

This is the most common way POST submits data. If the enctype property is not set in the browser's native form form form, the data will be submitted as application/x-www-form-urlencoded (the default POST method for enctype), and the submitted data will be encoded as key1=val1&key2=val2, with key and valL transcoded. Most server-side languages support this approach very well.

For example, the form:

<form action="form_action.asp">
  <p>First name: <input type="text" name="fname" /></p>
  <p>Last name: <input type="text" name="lname" /></p>
  <input type="submit" value="Submit" />
</form>

The request is as follows:

// Network Request
POST http://httpbin.org/post HTTP/1.1
Host    httpbin.org
User-Agent  python-requests/2.24.0
Accept-Encoding gzip, deflate
Content-Length  49
Content-Type    application/x-www-form-urlencoded;charset=utf-8
Connection  keep-alive

// Form Content
title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3		// Submitted data is encoded as key1=val1&key2=val2, keys and Vals are URL transcoded

2,multipart/form-data

This encoding, which is often used by clients to transfer large file data (such as pictures or files) to the server through a form, must make the enctype of the <form>form equal to multipart/form-data.

Example:

// Network Request
POST http://httpbin.org/post HTTP/1.1
Host: httpbin.org
User-Agent: python-requests/2.24.0
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=e48c73a7a42e403d868095dc3d060962
Content-Length: 222
Connection: keep-alive

// Encoded form content
--e48c73a7a42e403d868095dc3d060962
Content-Disposition: form-data; name="field0"

value1
--e48c73a7a42e403d868095dc3d060962
Content-Disposition: form-data; name="field1"

value2
--e48c73a7a42e403d868095dc3d060962--
Content-Disposition: form-data; name="field2"; filename="filename"
Content-Type: text/plain

ä½ å¥½ï¼Œè¥¿å›­å
¬å­ï½ž
--25c88ddc918d40e7a3cd5be0d62476b7--

Encoding method: The message body is divided into several similar parts by the number of fields, each starting with--boundary, followed by the key of the first key-value pair in the form, a newline, followed by the value (text or binary), and then a boundary string delimiter to split the different key values. Repeat the above operation. If you are transferring files, include file name and file type information. The message body ends with the--boundary--flag.

3,application/json

application/json This Content-Type serves as the request header to tell the server that the message body is a serialized JSON string. Due to the popularity of the JSON specification, all browsers except the lower version of IE support JSON.stringify natively, and server-side languages also have functions to handle JSON, so there is no trouble using JSON.
The JSON format supports much more complex structured data than key-value pairs and is particularly suited for RESTful interfaces.

Example:

JSvar data = {"name": "A playboy", "hobby": "coding", 'sub' : [1,2,3]};
$http.post(url, data).success(function(result) {
    ...
});

Request:

// Network Request
POST /post HTTP/1.1
Host: httpbin.org
User-Agent: python-requests/2.24.0
Accept-Encoding: gzip, deflate
Content-Type: application/json
Content-Length: 62
Connection: keep-alive

// Form Content
"{"name":"\\u516c\\u5b50\\u54e5","hobby": "coding","sub":[1,2,3]}"

4,text/xml

Not used at this time

Tags: Java

Posted on Tue, 30 Nov 2021 17:18:03 -0500 by fr0stx