Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
An automated data transfer between applications is made possible through webhooks. We can use a webhook to start a collection run in Postman When an event occurs or at a certain time.
In this article, we will discuss the introduction to webhooks, Triggering runs with webhooks, creating webhooks, accessing the request, Building requests workflows in postman, and Tips for building requests workflows in postman.
Webhooks
Webhooks are HTTP callbacks that provide message notifications for events. We can subscribe the webhook listener to particular events after you configure a webhook listener for it. There are resource collections for webhooks under the notifications namespace.
When a collection webhook triggers specific events, it provides data via a POST request to the webhook URL. You are responsible for configuring the data-sending application and the trigger events. The collection in the global object contains the data sent to the webhook. You can parse that data using scripts and use it in any way during the collecting process.
It is only possible to create webhooks for a collection using the Postman API.
Custom Webhooks in postman
Custom webhook integration in Postman allows you to automate processes between your favorite apps and services in order to receive notifications, synchronize files, collect data, and more. It provides services with predefined flows that you may use.
Postman allows you to build a custom webhook to broadcast events such as monitor results or team and collection-specific activity feeds, as well as to back up your Postman Collections.
Triggering runs with webhooks
Data can be automatically sent from one application to another via webhooks. Using a webhook, you can start a collection run in Postman at a certain time or when an event happens.
A custom payload can also be sent to the webhook and accessed when the collection runs, making it possible for the collection to operate without the aid of any environment and solely rely on the information given to the webhook.
Creating a webhook
When a collection webhook triggers specific events, it provides data via a POST request to the webhook URL. You are responsible for configuring the data-sending application and the trigger events. The collection in the global object contains the data sent to the webhook. You can parse that data using scripts and use it in any way during the collecting process.
It is only possible to create webhooks for a collection using the Postman API. Consult the api.getpostman.com/webhooks documentation to learn how to build a webhook.
Accessing the request body in the script
The webhook's request body is accessible via the globals. pastRequest object. Before using it, parse the globals. pastRequest object. As illustrated in the code sample below, the data supplied to the webhook is available in the data argument inside the parsed object.
// access the request body in script for workflows in postman
var previousdata = JSON.parse(globals.previousRequest),
webhookRequestData = previousRequest.data;
// Basically webhookRequestData contains the data that has to be sent to your webhook.
console.log(JSON.stringify(webhookRequestData));
Sending output to another API
Postman displays real-time test results and requests executions. Select a request's name from the results list to gain access to further information about what transpired when API executed it.
Here in this image, all the APIs are working in systematic order.
Here in this image, all the APIs are working in systematic order.
Building request workflows in postman
Postman typically executes all requests in the same order that appears in your collection when you start a collection run. The requests in the folder will execute first, then the rest of the requests will execute.
Before beginning a run, the Collection Runner allows you to alter the requests' sequence. However, you may automate this behavior by using Postman.setNextRequest() function rather than manually altering the requested order each time the collection is executed.
You can choose the request Postman makes after the current request using the function postman.setNextRequest(). You can create unique workflows that run requests sequentially in a chain.
Setting the next request
Add the following code to a request's Tests tab to indicate which request should be executed next. We should enter the name of the request we wish to execute next in place of request_name.
postman.setNextRequest("request_name");
The specified request will only execute when the postman executes the current request.
Looping Over a request
The current request will be executed repeatedly by Postman if the name of the current request is sent to the setNextRequest function.
Stopping a workflow
Add the following code to a request's Tests tab to end a workflow.
postman.setNextRequest(null);
Once Postman has finished processing the current request, the collection run will end.
Tips to build request workflows in postman
When using the postman.setNextRequest() function, keep the following points in mind.
setNextRequest() only works in Collection Runner
The postman.setNextRequest() function is solely utilized when running a collection and has no impact on requests sent via the Send method.
Use setNextRequest() in test scripts or pre-request
In the test scripts or pre-request for a request, you can use postman.setNextRequest(). The most recent value set has priority if several values are assigned.
Specifying the next request using the request ID
You can offer the request ID rather than the request's name to execute next. Open a request and click the information button in the context bar to the right to discover the request ID.
Keep in mind that the requested ID is listed after the user ID. Remove the dash and the first eight digits to obtain the request ID. Using the pm.info.requestId function, you can also obtain the request ID.
setNext request always executes last
The postman.setNextRequest() method is always called after processing the current request. After using this function, any more code blocks in the pre-request script or test script will still run before postman.setNextRequest().
setNext request() scope is limited to the collection
Your collection run's source is the range returned by postman.setNextRequest().
Any request in the collection, even those inside folders, can be configured as the next request if you execute the complete collection.
The scope of postman.setNextRequest() is restricted to the folder you are running when you use it. In this situation, you can set any request in the same folder as the one after it but no requests in other folders or at the collection's root.
Frequently Asked Questions
How do I configure my Postman's order?
The RUN Sequence section outlines the top-to-bottom order in which Postman will carry out the requests. (GET->POST->DEL->PUT). Select "Run Collection1" from the menu. According to the RUN ORDER, execution results reveal that Postman sent the GET request first, then POST, DEL, and lastly PUT.
How can I use a Postman loop to call API?
We can practice branching and looping in Postman. With the aid of the setNextRequest() function, we may branch and loop through different API calls. The request to be executed next is set using this function. The name of the request that will be executed next is the sole input this function accepts.
Can a Postman run a script?
Before a request is processed, JavaScript can be run using pre-request scripts in Postman. You can perform pre-processing tasks like adjusting variable values, parameters, headers, and body data by putting code in the Pre-request Script tab for a request, collection, or folder.
Conclusion
In this article, we have extensively discussed the introduction to webhooks, Triggering runs with webhooks, creating webhooks, accessing the request, Building requests workflows in postman, and Tips for building requests workflows in postman.
After reading about Triggering runs with webhooks and building request workflows in postman, are you not feeling excited to read/explore more articles on API testing tools? Don't worry; Coding Ninjas has you covered. If you want to check out articles related to API testing then you can refer to these links Introduction to the postman, postman for api testing, asynchronous data transfer, how api call works, API, API testing, and Web testing.