我正在使用邮递员Chrome扩展测试一个web服务。

有三种数据输入选项。

我猜raw是用来发送JSON的。

另外两个,form-data和x-www-form-urlencoded之间的区别是什么?


当前回答

下面是一些补充示例,用于查看Postman在请求中传递的原始文本。你可以通过打开Postman控制台看到这一点:

格式

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

Body

key1=value1key2=value2

x-www-form-urlencoded

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

Body

key1=value1&key2=value2

原始文本/平原

Content-Type: text/plain

Body

This is some text.

原始json

Content-Type: application/json

Body

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

其他回答

下面是一些补充示例,用于查看Postman在请求中传递的原始文本。你可以通过打开Postman控制台看到这一点:

格式

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

Body

key1=value1key2=value2

x-www-form-urlencoded

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

Body

key1=value1&key2=value2

原始文本/平原

Content-Type: text/plain

Body

This is some text.

原始json

Content-Type: application/json

Body

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

多部分/格式

请注意。有关文件上传的其他信息,包括向后兼容性问题、“multipart/form-data”与其他内容类型之间的关系、性能问题等,请参阅RFC2388。

有关表格安全问题的信息,请参阅附录。

内容类型“application/x-www-form-urlencoded”对于发送大量二进制数据或包含非ascii字符的文本是低效的。内容类型“multipart/form-data”应该用于提交包含文件、非ascii数据和二进制数据的表单。

内容类型“multipart/form-data”遵循RFC2045中列出的所有多部分MIME数据流的规则。“多部分/表单数据”的定义可在[IANA]登记处查阅。

“multipart/form-data”消息包含一系列部分,每个部分代表一个成功的控件。按照文档流中出现相应控件的相同顺序将部件发送到处理代理。部分边界不应该出现在任何数据中;如何做到这一点超出了本规范的范围。

与所有多部分MIME类型一样,每个部分都有一个可选的“Content-Type”头,默认为“text/plain”。用户代理应该提供“Content-Type”标头,并附带一个“charset”参数。

应用程序/ x-www-form-urlencoded

这是默认的内容类型。使用此内容类型提交的表单必须按以下方式编码:

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 `&'.

发送到服务器的HTTP消息主体本质上是一个巨大的查询字符串——名称/值对由&分隔,名称与值由等号分隔。一个例子是:

MyVariableOne=ValueOne&MyVariableTwo=ValueTwo

内容类型“application/x-www-form-urlencoded”对于发送大量二进制数据或包含非ascii字符的文本是低效的。内容类型“multipart/form-data”应该用于提交包含文件、非ascii数据和二进制数据的表单。

这些是W3C定义的不同表单内容类型。 如果你想发送简单的文本/ ASCII数据,那么x-www-form-urlencoded就可以了。这是默认值。

但如果您必须发送非ascii文本或大型二进制数据,form-data就是用于此的。

如果你想发送纯文本或JSON或任何其他类型的字符串,你可以使用Raw。顾名思义,邮差发送原始字符串数据,因为它是没有修改。您发送的数据类型可以通过使用下拉菜单中的content-type头来设置。

当您想要将非文本数据附加到请求时,可以使用二进制,例如视频/音频文件、图像或任何其他二进制数据文件。

进一步阅读请参考此链接: HTML文档中的表单

这解释得更好: 邮差文档

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.

更新

正如VKK指出的,WHATWG规范规定urlencoded是表单的默认编码类型。

这些属性的默认无效值是application/x-www-form-urlencoded状态。enctype属性缺少的默认值也是application/x-www-form-urlencoded状态。

让我们把一切都变得简单,这都是关于HTTP请求是如何发出的:

1 - x-www-form-urlencoded

http请求:

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 -生

http请求:

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 -格式

http请求:

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--