ChatGPT解决这个技术问题 Extra ChatGPT

Postman Chrome: What is the difference between form-data, x-www-form-urlencoded and raw

I am using the Postman Chrome extension for testing a web service.

There are three options available for data input.

I guess the raw is for sending JSON.

What is the difference between the other two, form-data and x-www-form-urlencoded?

As of today there is a 4th option as well to post data in postman tool - binary.
As of today there is a 5th option as well to post data in postman tool - GraphQL
As of today there is a 6th option as well to post data in postman tool - none

B
Basant Singh

These are different Form content types defined by W3C. If you want to send simple text/ ASCII data, then x-www-form-urlencoded will work. This is the default.

But if you have to send non-ASCII text or large binary data, the form-data is for that.

You can use Raw if you want to send plain text or JSON or any other kind of string. Like the name suggests, Postman sends your raw string data as it is without modifications. The type of data that you are sending can be set by using the content-type header from the drop down.

Binary can be used when you want to attach non-textual data to the request, e.g. a video/audio file, images, or any other binary data file.

Refer to this link for further reading: Forms in HTML documents


what about binary?
i have a form which has 4 text fields and 1 file upload field now please suggest me which option (form-data Or x-www-form-urlencoded) will be used to submit these 5 fields value in postman? Thanks in advance.
@Kamlesh, sorry for the late reply, form-data's key field in Postman has a dropdown where you can select whether it is a text field or file. You can set it to file and then browse a file to attach to the body. Hope that helps.
a
avck

This explains better: Postman docs

Request body While constructing requests, you would be dealing with the request body editor a lot. Postman lets you send almost any kind of HTTP request (If you can't send something, let us know!). The body editor is divided into 4 areas and has different controls depending on the body type. form-data multipart/form-data is the default encoding a web form uses to transfer data. This simulates filling a form on a website, and submitting it. The form-data editor lets you set key/value pairs (using the key-value editor) for your data. You can attach files to a key as well. Do note that due to restrictions of the HTML5 spec, files are not stored in history or collections. You would have to select the file again at the time of sending a request. urlencoded This encoding is the same as the one used in URL parameters. You just need to enter key/value pairs and Postman will encode the keys and values properly. Note that you can not upload files through this encoding mode. There might be some confusion between form-data and urlencoded so make sure to check with your API first. raw A raw request can contain anything. Postman doesn't touch the string entered in the raw editor except replacing environment variables. Whatever you put in the text area gets sent with the request. The raw editor lets you set the formatting type along with the correct header that you should send with the raw body. You can set the Content-Type header manually as well. Normally, you would be sending XML or JSON data here. binary binary data allows you to send things which you can not enter in Postman. For example, image, audio or video files. You can send text files as well. As mentioned earlier in the form-data section, you would have to reattach a file if you are loading a request through the history or the collection.

UPDATE

As pointed out by VKK, the WHATWG spec say urlencoded is the default encoding type for forms.

The invalid value default for these attributes is the application/x-www-form-urlencoded state. The missing value default for the enctype attribute is also the application/x-www-form-urlencoded state.


The Postman doc is wrong. WHATWG's HTML5 spec available at html.spec.whatwg.org/multipage/… clearly defines a default value of "application/x-www-form-urlencoded" for the enctype attribute of the form element. In other words "application/x-www-form-urlencoded" is the default encoding (in HTTP terms Content-Type) a web form uses to transfer data, not multipart/form-data. To send an HTTP post request on form submission with a Content Type of multipart/form-data, one must explicitly specify this as the enctype value.
So what is the difference between form-data (entered with key-value pairs in the Postman UI) sent with the Content-Type: application/json header; and raw data entered as json like {foo: bar} with the same Content-Type: application/json header?
With form-data as key value pairs content-type is multipart form-data even when you specify the headers & with raw content-type will be text or whatever you have specified in headers.
S
Suragch

Here are some supplemental examples to see the raw text that Postman passes in the request. You can see this by opening the Postman console:

https://i.stack.imgur.com/6gzgn.png

form-data

Header

content-type: multipart/form-data; boundary=--------------------------590299136414163472038474

Body

key1=value1key2=value2

x-www-form-urlencoded

Header

Content-Type: application/x-www-form-urlencoded

Body

key1=value1&key2=value2

Raw text/plain

Header

Content-Type: text/plain

Body

This is some text.

Raw json

Header

Content-Type: application/json

Body

{"key1":"value1","key2":"value2"}

what if you send {"key1":"value1","key2":"value2"} as raw text? Is it equivalent to using Raw json? I can't find somewhere stating the difference
@The body would be equivalent, but the Content-Type header would be misnamed if the server is looking for JSON.
The post did not address the difference between form-data and x-www-form-urlencoded.
C
Community

multipart/form-data

Note. Please consult RFC2388 for additional information about file uploads, including backwards compatibility issues, the relationship between "multipart/form-data" and other content types, performance issues, etc.

Please consult the appendix for information about security issues for forms.

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

The content type "multipart/form-data" follows the rules of all multipart MIME data streams as outlined in RFC2045. The definition of "multipart/form-data" is available at the [IANA] registry.

A "multipart/form-data" message contains a series of parts, each representing a successful control. The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream. Part boundaries should not occur in any of the data; how this is done lies outside the scope of this specification.

As with all multipart MIME types, each part has an optional "Content-Type" header that defaults to "text/plain". User agents should supply the "Content-Type" header, accompanied by a "charset" parameter.

application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type must be encoded as follows:

Control names and values are escaped. Space characters are replaced by +', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by %HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., %0D%0A'). The control names/values are listed in the order they appear in the document. The name is separated from the value by =' and name/value pairs are separated from each other by `&'.

application/x-www-form-urlencoded the body of the HTTP message sent to the server is essentially one giant query string -- name/value pairs are separated by the ampersand (&), and names are separated from values by the equals symbol (=). An example of this would be:

MyVariableOne=ValueOne&MyVariableTwo=ValueTwo

The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.


T
TechDogLover OR kiaNasirzadeh

let's take everything easy, it's all about how a http request is made:

https://i.stack.imgur.com/AkdtY.png

http request:

GET /getParam1 HTTP/1.1
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: a14f1286-52ae-4871-919d-887b0e273052
Host: localhost:12345
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 55

postParam1Key=postParam1Val&postParam2Key=postParam2Val

2- raw

https://i.stack.imgur.com/v8TiJ.png

http request:

GET /getParam1 HTTP/1.1
Content-Type: text/plain
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: e3f7514b-3f87-4354-bcb1-cee67c306fef
Host: localhost:12345
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 73

{
    postParam1Key: postParam1Val,
    postParam2Key: postParam2Val
}

3- form-data

https://i.stack.imgur.com/mR3vZ.png

http request:

GET /getParam1 HTTP/1.1
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: 8e2ce54b-d697-4179-b599-99e20271df90
Host: localhost:12345
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------140760168634293019785817
Content-Length: 181

----------------------------140760168634293019785817
Content-Disposition: form-data; name="postParam1Key"

postParam1Val
----------------------------140760168634293019785817--