preface
In order to send a picture to the back end, I used new FormData() to put the append in the picture and simulate form submission. I was in a hurry and thought I could have a happy meal after finishing it. The back end says no data was received… I passed the data to the back end, why did the back end say that it did not receive? So let’s get to the topic of this time
Introduction to the
What is Form Data? What is a RequestPayload? FormData and Payload are the two formats that the browser sends to the interface. The browser differentiates the two formats by the content-type value:
- If it is
application/x-www-form-urlencoded
If, isForm Data
way - If it is
application/json
ormultipart/form-data
If, isRequestPayload
The way.
inPOST
In the submission
Content-Type |
The request format |
---|---|
application/x-www-form-urlencoded |
formdata |
application/json |
request payload |
multipart/form-data |
request payload |
As you can see from this article on MDN MIME types: Multipart /form-data can be used to send HTML forms from the browser to the server. As a multipart document format, it consists of different parts separated by a boundary (a string beginning with a ‘–‘). Each part has its own entity, as well as its own HTTP request header, content-Disposition and Content-type for the file upload field, the most commonly used (content-length is ignored because the boundary is used as a delimiter). As shown in the figure below:
Form Data
Content-Type: multipart/form-data; boundary=aBoundaryString
(other headers associated with the multipart document as a whole)
--aBoundaryString
Content-Disposition: form-data; name="myFile"; filename="img.jpg"
Content-Type: image/jpeg
(data)
--aBoundaryString
Content-Disposition: form-data; name="myField"
(data)
--aBoundaryString
(more subparts)
--aBoundaryString--
Copy the code
Request Paload
What is the difference between request payload and formData in an HTTP request?
Here’s the explanation: FormData and Payload are two formats transmitted by the browser to the interface. The browser distinguishes the two formats by the content-type value. If the format is Application/X-wwW-form-urlencoded, it is form Data. If it is application/ JSON or multipart/form-data, the format is RequestPayload.
Reference links: stackoverflow.com/questions/2… The explanation is as follows:
When a request is accompanied, it might look like this:
POST /some-path HTTP/1.1
Content-Type: application/json
{ "foo" : "bar"."name" : "John" }
Copy the code
If you’re just making a simple Ajax request. The header of the request will be set to content-type: application/json. The browser will simply display your submission as payload, and that’s all it can do, because it doesn’t know where the data came from.
If you submit an HTML form with method=”post” and content-type: Application /x-www-form-urlencoded or Content-type: application/x-www-form-urlencoded Multipart/form – the data. Then your request might look something like this:
POST /some-path HTTP/1.1
Content-Type: application/x-www-form-urlencoded
foo=bar&name=John
Copy the code
Here, the form-data is the request payload. In this case, the browser knows more: it knows that bar is the value of the input field foo of the submitted form. That’s what it shows you.
The difference is that the content-type setting is different, not the way the data is submitted. Both types of commits put the data in message-body. But Chrome’s developer tools use this ContentType to differentiate the display.
payload
So what’s the difference? Why does the back end sometimes not get the value
payload
chrome
The traditional Form submission scenario is constructed as follows:
<form action="/" method="POST">
<input name="name" type="text">
<input name="password" type="text"</button> </form>Copy the code
If I click on the submit button here, it triggers the submit function in the browser, and what happens?
Pay attention to the point
Content-type: Application/X-www-form-urlencoded. The value is submitted in the form key1=value1&key2=value2.
Traditional Ajax commit scene construction
function submit2() {
var xhr = new XMLHttpRequest();
xhr.timeout = 3000;
var obj = {a: 1, b: 2};
xhr.open('POST'.'/');
xhr.send(obj);
}
Copy the code
First we construct a simple function, and then we fire it. From Chrome feedback:
traditionalajax
The submission requires the following considerations
- The default
The content-type
fortext/plain
. Request Payload
It does string conversions on non-strings.- through
xhr.send(JSON.stringify(obj))
; Can correct the content to send
Pay attention to the point
- When we pass a string,
Content-Type
Automatically converted toxxx-form-xxx
In the form. When an object, it is automatically converted toxxx/json
. - String
key1=val1&key2=val2
In the form ofJSON
It is represented as a string.
conclusion
Content-Type
The difference of
- The traditional
ajax
When you ask,Content-Typ
The default is eThe text
Type. - The traditional
form
At the time of submission,Content-Type
The default isForm
Type. axios
When I pass a string,Content-Type
The default isForm
Type.axios
When I pass an object,Content-Type
The default isJSON
Type.
Content-Type
The value of theForm
withThe Form
When,payload
The difference between:
- Both support string types only (several cases explored above)
Form
The format to be passed iskey1=value1&key2=value2
, similar toGET
The request ofQueryString
format- non
Form
As a general rule, beJSON.stringify(formDataObject)
In the form of
So if the back end can’t get a value, whatever form it passes, the back end will consider the Content-type when parsing the form information. If it’s a JSON string, the backend will parse the JSON when it parses the payload. If the string is of the form key1=value1&key2=value2, you need to defragment the string. These things are usually handled by the framework used by the backend, but the interface provided by the framework to the backend may be different, so the front-end partner must discuss with the back-end partner whether to use JSON or FormData to handle the request.
References:
- The MIME type
- Content-Type
- How to set a boundary on a multipart/form-data request while using jquery ajax FormData() with multiple files
Common media format types are as follows:
text/html
HTML format:text/plain
: Plain text formattext/xml
The XML format:image/gif
: GIF image formatimage/jpeg
: JPG image formatimage/png
: PNG Image format
In order toapplication
Media format type at the beginning:
application/xhtml+xml
XHTML format:application/xml
: XML data formatapplication/atom+xml
: Atom XML aggregation formatapplication/json
: Indicates a JSON data formatapplication/pdf
PDF format:application/msword
: Word document formatapplication/octet-stream
: Binary streaming data (such as a common file download)application/x-www-form-urlencoded
:< form encType = "" >
In the defaultencType
.form
The form data is encoded askey/value
Format to the server (the form’s default format for submitting data)
Another common media format is for uploading files:
multipart/form-data
: This format is used when you need to upload files in the form