{"id":55079,"date":"2022-06-26T12:16:42","date_gmt":"2022-06-26T06:46:42","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=55079"},"modified":"2022-07-05T00:19:00","modified_gmt":"2022-07-04T18:49:00","slug":"api-request-and-response-chaining-using-postman","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/api-request-and-response-chaining-using-postman\/","title":{"rendered":"API Request and Response chaining using Postman"},"content":{"rendered":"<p><span style=\"font-family: FreeSerif, serif;\"><i><b>What is an API?<\/b><\/i><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">API stands for Application Programming Interface which works as an intermediary between two applications to serve the request and responses.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">APIs are developed to speed up the functionality and request response mechanism for an applications.It allows to add functionality from other providers to the existing system, it can be different sources of information, verification services or communication services.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Basically, API essentially is a component that enables communication between two different applications, these applications could be developed differently like OS used can be different, Tech Stack can be different, DB used can be different.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">The API allows the developers to make a call\/request in order to send and receive some information from a server. The API uses JavaScript Object Notation to communicate over HTTP i.e. Hyper Text Transfer Protocol. HTTP requests are sent by client machines to get or receive some information from the server.It has four main elements namely \u2013 An HTTP method, Request type like GET, PUT, POST etc, Request HEAD\/OPTIONS and Request Body.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">These are the basic types of HTTP request-<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">1. GET<br \/>\n<\/span><span style=\"font-family: FreeSerif, serif;\">2. POST<br \/>\n<\/span><span style=\"font-family: FreeSerif, serif;\">3. PUT<br \/>\n<\/span><span style=\"font-family: FreeSerif, serif;\">4. DELETE<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">In this blog, we are going to learn the Request\/Response Chaining using one of the popular API testing tools i.e. POSTMAN-<\/span><\/p>\n<h2><span style=\"font-family: FreeSerif, serif;\">Problem Statement-<\/span><\/h2>\n<p><span style=\"font-family: FreeSerif, serif;\">While testing any API, we use to face a problem where we need to provide the Response of an API as the Request Parameter for another API.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">More precisely, We will learn to get a response from one API and pass it as request parameter in another API.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">In the current example, I am using 2 APIs to chain their request and responses.\u00a0<\/span><span style=\"font-family: FreeSerif, serif;\">The first API allows to upload a file and on the basis of that it generates a unique work-order id.\u00a0<\/span><span style=\"font-family: FreeSerif, serif;\">The second API uses the work order id to check the status of the uploaded document. <\/span><span style=\"font-family: FreeSerif, serif;\">We would need to follow the following steps to achieve the same-<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\"><i><b>Step 1:<\/b><\/i> <\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Go to Postman and Click on New -&gt; Collection -&gt; Add name to your collection say API_Chaining<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\"><i><b>Step 2:<\/b><\/i><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Create\/Add the first API requests which you want to chain and save it, it will give the response in the following format &#8211; <\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55076\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/1.png\" alt=\"\" width=\"974\" height=\"408\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/1.png 974w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/1-300x126.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/1-768x322.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/1-624x261.png 624w\" sizes=\"(max-width: 974px) 100vw, 974px\" \/><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">{<br \/>\n&#8220;MSG&#8221;: &#8220;Workorder Created&#8221;,<br \/>\n<\/span><\/span><\/span><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">&#8220;WORKORDER_ID&#8221;: &#8220;12345678909876&#8221;<br \/>\n<\/span><\/span><\/span><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">}<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\"><i><b>Step 3:<\/b><\/i> <\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Use Environment variable to parameterised the response value of API 1.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">To do so, Go to New &#8211; &gt; Environment and Provide any environment name<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Add a new Variable name say \u2018Dynamic_WorkOrderID\u2019 and provide some initial value like abc.<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Select the newly created environment from the top right corner.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55077\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/2.png\" alt=\"\" width=\"744\" height=\"253\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/2.png 744w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/2-300x102.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/2-624x212.png 624w\" sizes=\"(max-width: 744px) 100vw, 744px\" \/><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\"><i><b>Step 4:<\/b><\/i><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Now Add the Test Script to set the value of WorkOrder ID in \u2018Dynamic_WorkOrderID\u2019 variable<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Go to Test tab and add the script to set the value of work order id dynamically-<\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">bodyValue = JSON.parse(responseBody)<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">bodyData = bodyValue.WORKORDER_ID<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">console.log(bodyData)<\/span><\/span><\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">pm.environment.set(&#8220;Dynamic_WorkOrderID&#8221;, bodyData);<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">The first line <em><span style=\"color: #111111;\"><span style=\"font-size: medium;\">bodyValue = JSON.parse(responseBody)<\/span><\/span><\/em>, will fetch value from the response body of API 1<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Now we would need to provide the JSON path of the variable whose value we want to set in the environment variable dynamically-<\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">So, The second line is setting the value of WORKORDER_ID in bodyData variable<\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">bodyData = bodyValue.WORKORDER_ID<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">This statement simply prints the value which is set under bodyData variable<\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">console.log(bodyData)<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Now,We would need to set the value of bodyData variable in the environment variable i.e. Dynamic_WorkOrderID.<\/span><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">pm.environment.set(&#8220;Dynamic_WorkOrderID&#8221;, bodyData);<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\"><i><b>Step 5:<\/b><\/i><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">Now go to API 2 and set the body param as below &#8211;<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-55078\" src=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/3.png\" alt=\"\" width=\"979\" height=\"257\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2022\/06\/3.png 979w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/3-300x79.png 300w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/3-768x202.png 768w, \/blog\/wp-ttn-blog\/uploads\/2022\/06\/3-624x164.png 624w\" sizes=\"(max-width: 979px) 100vw, 979px\" \/><\/p>\n<p><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">{<br \/>\n&#8220;workorder_id&#8221;: &#8220;<i>{{Dynamic_WorkOrderID}}<\/i>&#8221;<br \/>\n<\/span><\/span><\/span><span style=\"color: #111111;\"><span style=\"font-family: FreeSerif, serif;\"><span style=\"font-size: medium;\">}<\/span><\/span><\/span><\/p>\n<p><span style=\"font-family: FreeSerif, serif;\">This will set the value of body parameter \u2018workorder_id\u2019 as per the value of environment variable value and the user need not to set it manually again and again to check the status for different work order ids and the user can simply hit the send request button in order to get the response from that API.<\/span><\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>What is an API? API stands for Application Programming Interface which works as an intermediary between two applications to serve the request and responses. APIs are developed to speed up the functionality and request response mechanism for an applications.It allows to add functionality from other providers to the existing system, it can be different sources [&hellip;]<\/p>\n","protected":false},"author":1467,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":131},"categories":[1994,1816],"tags":[4982,4984,4983],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55079"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1467"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=55079"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55079\/revisions"}],"predecessor-version":[{"id":55125,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/55079\/revisions\/55125"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=55079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=55079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=55079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}