How To Automate Rest API in Postman

How To Automate Rest API  in Postman 


1.  Let us take an example in which we need to create shipment 

Step 1 : login via username /password and then getting the access token in response . [ Login API ]
Step 2 : Creating the shipment each time with unique AWB no . [ automating AWB no in request ] and passing the access token in header generated via Step 1.  

login API

POST Request

http://XXX/oauth/token?grant_type=password&client_id=my-trusted-client&username=user1&password=pass1

Header
Content-Type : Application/json

Response

{
  "access_token": "eyJhbGciOiJIUzI1NiJ9.eyJleHBpcmF0aW9uIjoxNDc3MzE1NjMyMjc1LCJpc3N1ZWRBdCI6MTQ3NzMwNTYzMjI3NSwiaXNzdWVyIjoiT2xwQXV0aCIsInR5cGUiOiJhY2Nlc3NfdG9rZW4iLCJpc3N1ZWRGb3IiOiJha3NoYXkuYWdhcndhbEB1bmljb21tZXJjZS5jb20iLCJvdGhlckNsYWltcyI6e319.EmWoYEOXRjlloZlfM9ggHftxmnlowzPxFI9gPOAOnBk",
  "token_type": "bearer",
  "refresh_token": "eyJhbGciOiJIUzI1NiJ9.eyJleHBpcmF0aW9uIjoxNDc3NDA1NjMyMjc1LCJpc3N1ZWRBdCI6MTQ3NzMwNTYzMjI3NSwiaXNzdWVyIjoiT2xwQXV0aCIsInR5cGUiOiJyZWZyZXNoX3Rva2VuIiwiaXNzdWVkRm9yIjoiYWtzaGF5LmFnYXJ3YWxAdW5pY29tbWVyY2UuY29tIiwiY2xpZW50X2lkIjoibXktdHJ1c3RlZC1jbGllbnQiLCJ1c2VyX25hbWUiOiJha3NoYXkuYWdhcndhbEB1bmljb21tZXJjZS5jb20iLCJhdGkiOiJjZDcyOGM0ZS1lODExLTQzM2MtYTcxZi01NjY4NWE1ZTc4MDIiLCJzY29wZSI6WyJ0cnVzdCIsIndyaXRlIiwicmVhZCJdLCJvdGhlckNsYWltcyI6e319.79JiHhmNXSUvsRT4z9JibErkm_-fvuYEMqNuqPCXVq4",
  "expires_in": 9999,
  "scope": "read trust write",
  "companyId": null,
  "userId": null
}

The access token which is formed at the response needs to be stored in some variable of the postman and  is passed along in next subsequent request wherever necessary .

snapshot 1



Enter the below code in TESTS

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("token", jsonData.access_token);

It will create a new environmental variable named token which will store the value of access_token .




 2. Create Shipment API

POST Request

Header
Content-type : Application/json
Authorization : {{token}}                  

// here postman variable  value is replaced by -  {{token }}

Using the token value in header





 Automating the request with javascript [Random Generator ]


Consider the below request

{
"lengthInMm": 10,
"widthInMm": 10,
"city": "Gurgaon",
"state": "Haryana",
"countryCode": "IN",
"pincode": "122001",
"phone": "88 60 532795"
},
"paymentMethodCode": "COD",
"currencyCode": "INR",
"referenceNumber": "APRIL21008789611",
"trackingNumber": "SHIPMENTASG12",
"externalStatusCode": "IN_TRANSIT",
}


We need to generate unique tracking no as per request . so will try automate it.

In pre-request  enter the below code script

var text="shipment";
var charset = "abcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 8; i++ )
        text += charset.charAt(Math.floor(Math.random() * charset.length));
       
postman.setEnvironmentVariable("awb", text);

// it will set the value of awb with shipmentxflgmbm1 , 8 character random string is being generated











Comments

  1. Thanks for sharing good information with us. I love your blog and everything you share with us is up to date and very informative.
    Node JS Online training
    Node JS training in Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

ADD EMPTY HEADER IN JMETER SAMPLE REQUEST

Constructing Curl from Python , HTTP , PHP , Java Request