Tuesday, 29 May 2018

Postman: chaining requests

Most people already know how to send a lone request using Postman. Send it off, inspect the response you get from the server. Make some adjustments, and then try it again.
That’s all well and good. However, when you save one or more requests to a collection, you can start using some of the more advanced features in Postman. For example, with Postman, you can write scripts that run before and/or after you send your request and receive a response from the server.
There’s a lot more you can do with these scripts.
  • Do setup and teardown for testing. For example, you can do calculations to use in the main request or send asynchronous requests. Tests scripts are the cornerstone of testing in Postman – to validate your endpoint is working as expected.
  • Direct the next request to be called based on the results of the server response. Using basic JavaScript, you can then use postman.setNextRequest("Request name") to guide the sequence of the remaining API calls.
  • Extract data from the response. This can be useful to capture session or cookie data or parse information available within the response body to chain requests. In this tutorial, we’ll dig deeper into this scenario.

Set up a Bitcoin tracker

Have you heard of Bitcoins?!? It's one of many cryptocurrencies based on blockchain technology, something about maths, and… well, there's probably more I should know about that. What I did know, however, is someone who accidentally purchased 200 Ethereum (another cryptocurrency), instead of $200 worth of Ethereum. With this fortunate mistake, he made enough money to pay for his son's college tuition over the course of 2 weeks  
How can you make money when the price of Bitcoins are so volatile? Turns out, you can make money because the price of Bitcoins are so volatile.
I had serious fear of missing out (FoMO), and wanted to gamble on invest in Bitcoin too! The next time the price of Bitcoin fell to a reasonably low rate, I planned to fill my coffers with Bitcoins like Scrooge McDuck.
Let's walk through how to extract data from a response and chain together requests. 
  1. Create and select an environment.
    • For this example, we’ll create and select a new environment. We’ll use this environment to store and access our environment variables. Update the values using your own Twilio API credentials. We'll be using the Twilio API to send ourselves an SMS when the price of Bitcoin falls to certain price. Sign up for a Twilio test account if you don't already have one.
  2. Send a GET request, extract data from the response, and store as an environment variable.
    • Send a GET request  to https://api.coindesk.com/v1/bpi/currentprice.json using Postman. This request returns a JSON body. Remember to save this request to a collection. We'll see why this is important in just a bit.
    • Under the Tests tab, add the following JavaScript. The important thing to know here is that we can access the JSON response body using pm.response.json(), and then parse the response object to extract the current Bitcoin rate. Then we can store this value as an environment variable using pm.environment.set("rate", usdRate) where "rate" is the name of our environment variable and usdRate is the value that we'll store as this variable. 
    • After we hit Send, click on the Quick Look icon to see that we’ve extracted the data from the response body, and saved it as an environment variable. Now, we can use this variable in our next request.
    • In the same manner, can you figure out how to extract the "time" from the response, and then save it as an environment variable?
  3. Send a POST request using a stored environment variable. 
    • For this second request, we will use the Twilio API to send an SMS. Send a POST to https://api.twilio.com/2010-04-01/Accounts/{{twilioAccountSID}}/Messages.jsonwhere {{twilioAccountSID}} is your own Twilio Account SID. Remember to also save this second request to our collection.
    • String substitution: we can use the double curly braces syntax anywhere we would normally input text, like {{variable-name}}, to use a variable. Postman will access the variable's value and swap in the value as a string. Use the double curly braces in the URL, parameters, or any of the other data editors.
    • More String Substitution: this request requires the authentication credentials we saved to our environment in the first step. We can use the same double curly braces syntax in Postman's auth helper to handle Basic Auth for the Twilio API. Authentication (and scripts) are available for entire collections and folders too.
    • Special note about accessing variables in scripts: there's a different way to access variables in the script sections since we're writing JavaScript, instead of text. Under the pre-request or tests script tabs, use pm.variable.get("variable-name").
  4. Run the collection
    • Instead of sending your requests one at a time, you can run every request in the collection in one go. Run the collection locally with collection runner in the Postman app or with the Newman command line tool. Alternatively, you can run the collection and environment on the Postman cloud as a monitor.
Postman Tip: you can send these requests in the default, ordered sequence, or you can write a script to execute these calls in a specified sequence using postman.setNextRequest("Request name") or to terminate the remaining collection run using postman.setNextRequest(null). In this manner, you can build a sequence of API calls that mirror your use case.

Try it yourself

Click the orange Run in Postman button to import the Bitcoin Tracker collection and environment into your Postman app. 
Run in Postman
  1. Enter your Twilio credentials: You will need a Twilio Account SID to update the twilioAccountSID value within the Postman environment. You will need a Twilio Auth Token to update the twilioAuthToken value within the Postman environment.
  2. Enter a target rate: In this example, set a target rate by updating the targetRate value within the Postman environment. If the current Bitcoin rate drops below this target, like in a flash crash, the POST request to the Twilio API will be called.
  3. Run it: Run the collection and environment locally using the Postman collection runner.

Need help getting started? Check out the documentation for the Bitcoin Tracker, and follow along with step-by-step instructions, examples of responses, and helpful screenshots.


No comments:

Post a Comment

What to do to make sure HTML Table is loaded completely if implicit and explicit wait are not working.

These days I was working on a project, to implement the delete function of a listing. Such as below: Actually,  every time deleted the it...