How To Use Postman To Set Key And Value
Setting Key-Value Pairs in Postman for API Testing
Postman is a powerful tool for API testing, and one of its key features is the ability to manage and send data with key-value pairs. This is essential for many API interactions, allowing you to send parameters, headers, and even entire request bodies in a structured format. Here’s a comprehensive guide on how to set key-value pairs in Postman, including practical examples.
1. Setting Key-Value Pairs in Request Headers
Request headers are useful for providing additional information about the request, such as authentication tokens, content types, and caching preferences.
-
Open a new request: Click the “New” button and choose “Request” to create a new request in Postman.
-
Set the URL: In the request builder, enter the API endpoint you want to test in the “Enter request URL” field.
-
Navigate to the “Headers” tab: Click the “Headers” tab in the request builder.
-
Add key-value pairs:
- Click the “Add key” button or simply start typing a new key in the “Key” field.
- Enter the corresponding value in the “Value” field.
Example:
Let’s say you need to set an Authorization
header for a request requiring authentication.
- Key:
Authorization
- Value:
Bearer [your_access_token]
Sample Code:
{ "Authorization": "Bearer your_access_token"}
2. Setting Key-Value Pairs in Request Parameters
Request parameters are used to pass data along with the URL, modifying the request’s behavior or fetching specific data.
-
Open a new request: Create a new request as described in step 1.
-
Select the “Params” tab: Click the “Params” tab in the request builder.
-
Add key-value pairs:
- Click the “Add param” button or type the key and value directly into the input fields.
- Enter the parameter name in the “Key” field and the corresponding value in the “Value” field.
Example:
Let’s say you’re making a request to a /products
endpoint to retrieve products matching specific criteria.
- Key:
category
- Value:
electronics
Sample Code:
{ "category": "electronics"}
3. Setting Key-Value Pairs in Request Body (JSON)
For requests where you need to send structured data, like when creating or updating resources, you’ll often use a JSON request body.
-
Open a new request: Create a new request as described in step 1.
-
Set the “Body” tab: Click the “Body” tab in the request builder.
-
Choose “raw” and select “JSON” from the dropdown menu: In the “Body” section, choose “raw” as the body type and select “JSON” from the dropdown menu.
-
Add key-value pairs:
- Type your JSON code directly into the body field. In JSON, key-value pairs are enclosed in curly braces ({}), keys are strings enclosed in double quotes (""), and values can be of various data types (strings, numbers, booleans, arrays, etc.).
Example:
Consider creating a new user with the following data:
{ "firstName": "John", "lastName": "Doe", "email": "john.doe@example.com"}
4. Dynamic Key-Value Pairs Using Variables
Postman allows you to store values in variables and reference them when setting key-value pairs. This provides flexibility, allowing you to easily update your tests and automate data retrieval.
-
Create a new variable: Go to the Postman environment and click “Add a variable” to create a new variable.
- Key: The variable name (e.g.,
apiKey
). - Value: The value you want to store (e.g.,
your_api_key
).
- Key: The variable name (e.g.,
-
Reference the variable:
- In any of the key-value pair fields we discussed earlier (headers, parameters, or body), type
{{apiKey}}
(replaceapiKey
with your variable name) to reference the corresponding variable.
- In any of the key-value pair fields we discussed earlier (headers, parameters, or body), type
Example:
- Using a variable
baseUrl
to make requests to different environments:
{ "url": "{{baseUrl}}/products"}
5. Setting Key-Value Pairs with Collections
Postman’s collections provide a structured way to organize API requests and related data. Collections support variables and environments, making them ideal for managing complex sets of requests.
- Create a new collection: Click the “New” button and choose “Collection” to create a new collection.
- Add a request: Add a request to your collection as described in steps 1-4.
- Set variables in the collection:
- Click on the collection name in the left sidebar to open the collection settings.
- Click the “Variables” tab and add key-value pairs representing variables specific to this collection.
- Reference variables: Use the
{{variable_name}}
notation within your requests to pull in the values from the collection’s variables.
6. Setting Key-Value Pairs in Tests
While setting key-value pairs in request definition is crucial, Postman’s test scripts let you manipulate and verify data dynamically.
- Create a test script: In the “Test” tab of your request, write JavaScript code using Postman’s built-in assertions and functions.
pm.test("Status code is 200", function () { pm.response.to.have.status(200);});
// Extract a value from the response bodylet userId = pm.response.json().id;
// Set a new variablepm.environment.set("userId", userId);
// Use the variable for future requestspm.sendRequest("{{baseUrl}}/users/{{userId}}");
This example demonstrates how to access response data, set variables within the test, and use them for subsequent requests within the same collection.
By mastering these techniques for setting key-value pairs in Postman, you’ll unlock the full power of this tool for API testing, enabling you to send complex requests, verify data integrity, and build robust automation workflows.