Difference Between form-data, x-www-form-urlencoded and raw in Postman – Postman中form-data、x-www-form-urlencoded和raw之间的区别

最后修改: 2022年 7月 1日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Postman provides multiple ways to interact with an API or server request with different types of body parameters. These represent different ways of sending data through an HTTP request to the API.

Postman提供了多种与API或服务器请求进行交互的方式,其中包括不同类型的主体参数。这些代表了通过HTTP请求向API发送数据的不同方式。

In this tutorial, we’ll explore the differences between using form-data, x-www-form-urlencoded, and raw for our request body.

在本教程中,我们将探讨在请求正文中使用form-datax-www-form-urlencodedraw的区别。

2. form-data

2.表格数据

Form-data represents data sent from website forms to APIs as part of multipart/form-data. The form-data option in Postman simulates filling up a form on a website and submitting it. We can edit the form data and let him set the different key/value pairs by transforming the key-value editor in the data.

Form-data表示从网站表单发送到API的数据,作为multipart/form-data的一部分。 Postman中的form-data选项模拟了在网站上填写表单并提交。我们可以编辑表单数据,让他通过转换数据中的键值编辑器来设置不同的键值对。

This can also be used for attaching files to the keys as well. However, one should note that using HTML5 restricts that files are not in any history or collections. Therefore we must select the file again at the time of sending the request body. Also, uploading multiple files with their content type isn’t supported by Postman.

这也可以用于将文件附在钥匙上。然而,人们应该注意到,使用HTML5限制了文件不在任何历史或集合中。因此,我们必须在发送请求体的时候再次选择文件。另外,上传多个文件的内容类型也不被Postman所支持。

Note that Postman will persist the file paths with subsequent use, i.e., when we repeatedly make an API call that sends the same file onto the server. This helps in running collections with multiple requests having file upload.

请注意,Postman会在以后的使用中保持文件路径,也就是说,当我们重复进行API调用,将同一个文件发送到服务器上时。这有助于运行具有文件上传的多个请求的集合。

Let’s see what using form-data looks like in Postman:

让我们看看在Postman中使用form-data是什么样子。

form-data

form-data

3. x-www-form-urlencoded

3.x-www-form-urlencoded

The URL-encoded data sends encoded data to the server and uses the same encoding as that of the URL parameters. To use it, we need to select the x-www-form-urlencoded tab in the body of their request. We need to enter the key-value pairs for sending the request body to the server, and Postman will encode the desired data before sending it. Postman encodes both the key and the value.

URL-encoded数据向服务器发送编码数据,并使用与URL参数相同的编码。要使用它,我们需要在其请求主体中选择x-www-form-urlencoded标签。我们需要输入用于向服务器发送请求正文的键值对,Postman将在发送之前对所需数据进行编码。Postman对键和值都进行了编码。

Note that it cannot be used for encoding files, so we manually need to do it ourselves. However, it can only encode the request body data or the URL parameters.

请注意,它不能用于对文件进行编码,所以我们需要自己手动完成。然而,它只能对请求正文数据或URL参数进行编码。

This is also known as the default content type. All the forms submitted with this content type follow the below encoding pattern:

这也被称为默认的内容类型。所有用这种内容类型提交的表单都遵循以下编码模式。

  • The control names and the values are escaped – all space characters will be replaced by the ‘+’ symbol, and reserved characters follow the RFC 17.38 notations.
  • An equal symbol, i.e., ‘=’ is used for separating the key and value and the key/value pairs use ‘&’ to be separated from each other.

Let’s take a look at the x-www-form-urlencoded tab in Postman:

让我们看一下Postman中的x-www-form-urlencoded标签。

Screenshot-2022-06-18-at-11.32.41-PM

Screenshot-2022-06-18-at-11.32.41-PM

4. raw

4.raw</em

As the name suggests, raw data can consist of anything. Postman does not touch the raw string or make any type of alterations to it. The string added into the raw editor goes unmodified except for replacing the defined environment variables. This editor lets us set different formatting styles supported by the Postman along with the correct header that is required to be sent with the raw body. The following types are supported:

顾名思义,raw数据可以由任何东西组成。Postman不接触raw字符串或对其进行任何类型的修改。添加到raw编辑器中的字符串除了替换已定义的环境变量外,不做任何修改。这个编辑器让我们可以设置Postman所支持的不同的格式化风格,以及需要与raw正文一起发送的正确的标题。

  • Text
  • Javascript
  • JSON
  • HTML
  • XML

We can also set these Content-Type manually into our request body:

我们也可以在我们的请求正文中手动设置这些内容-类型。

Screenshot-2022-06-18-at-11.50.07-PM

Screenshot-2022-06-18-at-11.50.07-PM

5. Difference BETWEEN form-data, x-www-form-urlencoded and raw

5.BETWEEN form-data, x-www-form-urlencodedraw 的区别

We can use the different forms of data content types as defined by the W3C committee. They have defined multiple formats for sending the data over the Network Layer. These include the form-data, x-www-form-urlencoded, and raw data. By default, we can send data in simple text/ASCII format by using the x-www-form-urlencoded format.

我们可以使用由W3C委员会定义的不同形式的数据内容类型。他们已经定义了多种格式,用于在网络层上发送数据。这些格式包括form-data、x-www-form-urlencodedraw数据。默认情况下,我们可以通过使用x-www-form-urlencoded格式来发送简单文本/ASCII格式的数据。

However, using the x-www-form-urlencoded data type has a data limit. Therefore, we can use form-data for sending large binary or non-ASCII text to the server.

然而,使用x-www-form-urlencoded数据类型有一个数据限制。因此,我们可以使用form-data来向服务器发送大型二进制或非ASCII文本。

The raw data type sends any plain text or JSON to the server, as the name suggests. It supports multiple content types, and Postman will send the raw data without any modifications compared to the other data types.

raw数据类型将任何纯文本或JSON发送到服务器,正如其名称所示。它支持多种内容类型,与其他数据类型相比,Postman将发送原始数据而不作任何修改。

We can use the raw data type to send any type of data in the request body. This also includes sending the Javascript functions that could be executed on the server side. We can send the scripts under the Javascript option. The raw data type also supports markup languages such as HTML and XML. This can be helpful when there is no logic at the front end and we need to consume the whole HTML/XML page.

我们可以使用raw数据类型来发送请求体中的任何类型的数据。这也包括发送可以在服务器端执行的Javascript函数。我们可以在Javascript选项下发送这些脚本。raw数据类型也支持标记语言,如HTML和XML。当前端没有逻辑,而我们需要消费整个HTML/XML页面时,这可能很有帮助。

6. Conclusion

6.结论

In this article, we’ve seen a few of the request body data types supported by Postman.

在这篇文章中,我们已经看到了Postman所支持的一些请求体数据类型。

We also outlined the differences between form-data, x-www-form-urlencoded and raw in Postman. However, we have only understood the basics of requests. You explore the Postman online documentation to understand these request body types more deeply.

我们还概述了Postman中form-datax-www-form-urlencodedraw之间的区别。然而,我们只了解了请求的基本情况。您可以探索Postman在线文档来更深入地了解这些请求体类型。