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.
In this tutorial, we'll explore the differences between using form-data, x-www-form-urlencoded, and raw for our request body.
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.
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.
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.
Let's see what using form-data looks like in Postman:
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.
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.
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:
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:
We can also set these Content-Type manually into our request body:
5. Difference between form-data, x-www-form-urlencoded and raw
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.
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.
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.
In this article, we've seen a few of the request body data types supported by 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.