Skip to content

How To Use Form Data In Postman

API Testing Blog

Sending Form Data in Postman

Postman is a powerful tool for testing APIs, and one of its key features is the ability to send form data in requests. Form data is often used for submitting data to web applications, such as login credentials, user registration details, or product information.

This guide will walk you through the process of sending form data in Postman, covering various methods to suit different API requirements.

Using the Form Data Tab

The most intuitive way to send form data is through Postman’s dedicated “Form Data” tab.

Step 1: Selecting the Form Data Tab

  1. In the Postman interface, select the request you’re working with.
  2. Navigate to the “Body” tab.
  3. Select “form-data” from the drop-down menu.

Step 2: Adding Form Fields

  1. Enter the key (name of the form field) in the “Key” column.
  2. Enter the value for the key in the “Value” column.
  3. You can add multiple fields by clicking the “Add” button.
  4. Optionally, you can define the file type for each field by selecting “File” from the “Type” drop-down.

Sample Code: Sending User Registration Data

Request:

POST /users/register HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="username"
johndoe
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="password"
password123
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="email"
johndoe@example.com
------WebKitFormBoundary7MA4YWxkTrZu0gW--

Postman Interface:

KeyValueType
usernamejohndoetext
passwordpassword123text
emailjohndoe@example.comtext

Sending Raw Form Data

Sometimes, you might need to send form data in a raw format, especially if the API doesn’t support the “form-data” content type.

Step 1: Select the “raw” Tab

  1. In the “Body” tab, select “raw” from the drop-down menu.

Step 2: Define the Content Type

  1. Choose the appropriate Content Type from the drop-down menu.
  2. Typically, for form data, you’ll use “application/x-www-form-urlencoded”.

Step 3: Enter the Form Data

  1. Enter the form data in the text area, following the standard encoding format: key1=value1&key2=value2&key3=value3.

Sample Code: Sending Login Credentials

username=johndoe&password=password123

Postman Interface:

Body Tab:

username=johndoe&password=password123

Headers Tab:

KeyValue
Content-Typeapplication/x-www-form-urlencoded

Sending File Uploads

Postman allows you to upload files as part of form data.

Step 1: Select the “File” Type

  1. In the “Form Data” tab, select “File” from the “Type” drop-down for the relevant field.

Step 2: Choose the File

  1. Click the “Choose File” button and browse to the file you want to upload.

Sample Code: Uploading a Profile Picture

Postman Interface:

KeyValueType
usernamejohndoetext
profile_pic(file path)file

Using the FormData Object in JavaScript

For more complex scenarios or when you need to create form data dynamically, you can use Postman’s JavaScript pre-request script.

Step 1: Create a New Pre-request Script

  1. Click the “Pre-request Script” tab.

Step 2: Create a FormData Object

var formData = new FormData();
// Add form fields
formData.append('username', 'johndoe');
formData.append('password', 'password123');
// Add a file
var file = pm.request.files.get('profile_pic');
formData.append('profile_pic', file.data, file.name);
// Set the request body with the form data
pm.request.body = formData;

Sample Code: Dynamic Form Data Creation

This example demonstrates creating form data dynamically based on user input.

var username = pm.environment.get('username');
var password = pm.environment.get('password');
var formData = new FormData();
formData.append('username', username);
formData.append('password', password);
pm.request.body = formData;

Conclusion

Sending form data in Postman offers flexibility and control over your API testing process. By utilizing the “Form Data” tab, raw data options, and JavaScript scripting, you can effectively interact with APIs that accept form data, regardless of the complexities involved. Remember to adapt the methods and code snippets to your specific API requirements and testing scenarios.

API Testing Blog