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 the data sent from website forms to APIs as part of multipart/form-data. The form-data option in Postman simulates filling out a form on a website and submitting it. We can edit the form data and let it 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, we should note that using HTML5 means that the files aren't 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 to upload files.
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 the 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 can't be used for encoding files, so we need to manually 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 values are escaped. All space characters will be replaced by the ‘+' symbol, and reserved characters follow the RFC 17.38 notations.
- An equal symbol, ‘=', 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 doesn't 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's required to be sent with the raw body. The following types are supported:
We can also set these Content-Types 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've defined multiple formats for sending the data over the Network Layer. These include 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. As such, 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, unlike with the other data types.
In this article, we examined 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 only gained a basic understanding of the requests. If you want to understand these request body types more deeply, you can explore the Postman online documentation.