Postman is a powerful tool for testing APIs, and a common requirement is sending structured data like arrays in your requests. While not always immediately obvious, Postman provides several ways to achieve this. This tutorial will cover the primary methods for sending arrays with Postman, along with explanations and examples to get you started.
Understanding How Postman Handles Arrays
The key to successfully sending arrays lies in how you format your request body and headers. Postman doesn’t inherently recognize "array" as a data type in the same way a programming language does. Instead, you need to represent the array structure using formats that the API can interpret, typically JSON.
Method 1: Sending JSON Arrays
The most common and recommended approach is to send data as a JSON object containing an array. This is especially suitable when using the raw
request body format.
-
Set the Content-Type: In the Postman
Headers
tab, add a header with the keyContent-Type
and the valueapplication/json
. This tells the server that the request body is formatted as JSON. -
Use the Raw Request Body: Select
raw
as the request body type. ChooseJSON
from the dropdown menu to enable JSON syntax highlighting and validation. -
Format the Array in JSON: Construct a JSON object where one of the keys holds the array. Here’s an example:
{ "user_ids": ["1234", "5678"] }
In this example,
user_ids
is the key, and the value is a JSON array containing two string IDs. Ensure your array values are appropriately quoted (strings) or formatted (numbers, booleans) according to the API’s expectations.
Method 2: Using Form-Data with Array Syntax
Postman’s form-data
format can also be used to simulate arrays, but it requires a specific syntax. This method is less common for complex data but can be useful in certain scenarios.
-
Select
form-data
as the request body type. -
Add keys and values that represent the array elements. Use the following patterns:
key[]
: This will create a simple array with the values assigned to the same key.key[index]
: This allows you to specify the index of each element in the array, creating a more structured array.key[index][]
: This will create an array of arrays
For example, to send an array named
box
with values "a", "b", "c", and "d", you would configure the form data as follows:| Key | Value |
|———–|——-|
| box[] | a |
| box[] | b |
| box[n1] | c |
| box[n1] | d |The server might receive this data as a nested structure like:
{"box":{"0":"a","n1":["c","d"]}}
.
Method 3: Sending Arrays of Objects
Often, you’ll need to send an array of objects, where each object represents a complex data structure. This is easily achieved using the JSON format described in Method 1.
[
{
"reason": "scrolled",
"tabid": "2"
},
{
"reason": "reload",
"tabid": "1"
}
]
In this case, the request body is a JSON array, and each element within the array is a JSON object with key-value pairs.
Important Considerations
- API Expectations: Always consult the API documentation to understand the expected format for arrays. Some APIs might require a specific structure or naming convention.
- Data Types: Ensure that the data types within your array are compatible with the API’s requirements. For example, if the API expects numbers, make sure your array contains numerical values and not strings.
- Validation: Utilize Postman’s built-in JSON validation feature to catch syntax errors before sending the request.
- Content-Type: Setting the correct
Content-Type
header is crucial for the server to correctly interpret the request body.