As an API development tool, Postman is widely used by developers for testing, documenting, and sharing APIs. One of the most powerful features of Postman is the ability to execute code before a request is sent, which is known as a pre-request script. This functionality allows developers to manipulate request parameters, add dynamic variables, and perform various computations or validations before the actual API request is made.
Environmental variables
In addition to pre-request scripts, Postman also offers a powerful feature called environmental variables. Environmental variables allow developers to store and reuse values across multiple requests and collections. This functionality can be especially useful when dealing with complex APIs that require authentication tokens, API keys, or other dynamic variables that change over time.
To use environmental variables in Postman, developers can define a set of
variables in an environment file. An environment file is simply a
collection of key-value pairs that can be accessed by all requests within a
collection. For example, an environment file for a testing environment
might include variables like base_url
and api_key
. These variables can
then be accessed by individual requests by using double curly braces and
the variable name like {{base_url}}/users
.
URL parameters using addQueryParams
In Postman, the addQueryParams
function is a JavaScript function that
allows developers to easily add query parameters to a URL. Query parameters
are often used in APIs to filter or paginate results, and can be added to
the URL as key-value pairs. Example:
pm.request.url.addQueryParams({ page: 1 })
Using use the addQueryParams()
function in Postman, developers can create
a pre-request script and use the pm.request.url
object to manipulate the
URL before sending the request.
Encoding URI components
In JavaScript, the encodeURIComponent()
function is used to encode
special characters in a URL. When a URL contains special characters like
spaces, ampersands, or slashes, they can cause issues when passed as a
parameter in a request. The encodeURIComponent()
function ensures that
these special characters are properly encoded, so they can be safely used
in a URL.
To use the encodeURIComponent()
function in JavaScript, simply pass the
string that needs to be encoded as the function's argument. For example,
the following code encodes the string hello world
:
let encodedString = encodeURIComponent("hello world")
The resulting encoded string would be "hello%20world". Notice how the space character is replaced with "%20", which is the encoded representation of a space in a URL.
The encodeURIComponent()
function can also be used in conjunction with
the addQueryParams()
function in Postman's pre-request scripts, to
properly encode query parameter values. For example, if a query parameter
value contains a special character, like a space or an ampersand, it needs
to be properly encoded before it can be added to the URL. The following
script demonstrates how to properly encode a query parameter value before
adding it to the URL:
let queryParamValue = "hello world & goodbye"
let encodedQueryParamValue = encodeURIComponent(queryParamValue)
pm.request.url.addQueryParams({ param: encodedQueryParamValue })
In this example, the encodeURIComponent()
function is used to encode the
value of the queryParamValue
variable before it is added as a query
parameter to the URL.
Overall, the encodeURIComponent()
function is a simple yet powerful
function in JavaScript that can be used to properly encode special
characters in a URL. When used in conjunction with Postman's pre-request
scripts, it can help ensure that APIs are properly tested and function as
expected, even when dealing with special characters in query parameters or
URL segments.
Dealing with JSON objects
In JavaScript, the JSON.stringify()
function is used to convert a
JavaScript object into a JSON string. JSON (short for JavaScript Object
Notation) is a lightweight data-interchange format that is commonly used in
web applications for transmitting data between the client and the server.
To use the JSON.stringify()
function, simply pass the object that needs
to be converted as the function's argument. For example, the following code
converts a JavaScript object into a JSON string:
let obj = { name: "John", age: 30 }
let jsonString = JSON.stringify(obj)
console.log(jsonString)
The resulting JSON string would be {"name": "John", "age": 30}
. Notice
how the object properties are converted into a string with the key-value
pairs separated by colons, and the pairs separated by commas. The resulting
JSON string can be easily transmitted over a network and then parsed back
into a JavaScript object on the receiving end.
The JSON.stringify()
function can also be useful in Postman's pre-request
scripts, where it can be used to convert an object into a string before it
is sent in a request body. For example, the following script demonstrates
how to use the JSON.stringify()
function to convert an object into a
string before sending it in a request:
const key = pm.environment.get("API_KEY")
const email = pm.environment.get("EMAIL")
const init = JSON.stringify({ key, email })
pm.request.addQueryParams("data=" + encodeURIComponent(init))
That's it. Enjoy!