3

In Postman, I am trying to retrieve a session value returned from a post call and use it in another get call.

I mean, I called via Postman:

curl -X POST \
  https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0 \
  -H 'Accept: */*' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Host: skyscanner-skyscanner-flight-search-v1.p.rapidapi.com' \
  -H 'Postman-Token: xxxx' \
  -H 'User-Agent: PostmanRuntime/7.15.0' \
  -H 'accept-encoding: gzip, deflate' \
  -H 'cache-control: no-cache' \
  -H 'content-length: 177' \
  -d 'inboundDate=2019-11-25&cabinClass=economy&children=0&infants=0&Country=BR&Currency=BRL&locale=pt-BR&originPlace=GRU-sky&destinationPlace=MCZ-sky&outboundDate=2019-11-19&adults=2'

It returned in Header among others keys:

Location →http://partners.api.skyscanner.net/apiservices/pricing/uk2/v1.0/b5b906e9-164e-4f1d-b5ce-9085b8a3506d

Now, I want to call another API and I must use the session id returned in previous call. It is after /v1.0/. In this case it is b5b906e9-164e-4f1d-b5ce-9085b8a3506d

The first steps is getting the Location key value but I am getting the error mentioned in this question title (see picture attached for more details).

So my straight question is how can I retrieve the Location value from another post request? Well, a complementary and helpfull coment will be how to parse and get only the session id but naturally firsly I have to fix how retrieve the whole Location value.

It is more or less this what I am trying:

var sessionKey = JSON.parse(postman.getResponseHeader('Location') );//maybe here applying some regular expression to get only the value after the last "/"
pm.environment.set("sessionkey", postman.getResponseHeader("sessionkey"));

My question is somehow close to In POSTMAN how do i get substring of response header item? but here the question author is able to use postman.getResponser and it seems he didn't get a final answer at the end.

My Postman version is 7.3.4 (Latest) (I have just updated it).

print

*** edited after first sugestion

enter image description here

*** another tentative failling

var locationHeader = pm.response;
console.log(pm.response); //Print successfully all response including body and header
var jsonData = JSON.parse(pm.response); //exception: There was an error in evaluating the Pre-request Script:  JSONError: Unexpected token u in JSON at position 0
console.log("Location = " + jsonData.Location);

PS. it is interesting that

pm.request.headers.add({key: 'X-RapidAPI-Host', value: 'skyscanner-skyscanner-flight-search-v1.p.rapida.com'})

works properly in my collection Pre-request scripts. It is obvious to have pm.request.headers.get, isn't it? But it doesn't have.

*** edited

I also added this question in https://community.getpostman.com/t/trying-to-get-a-session-value-from-a-post-call-in-postman-typeerror-postman-getresponseheader-is-not-a-function/6804

*** edited

Just in case someone get interested, my final implementation is

var locationHeader = pm.response.headers.get("Location").split('/');

var locationPosition = locationHeader.length-1;

var sessionKey  = locationHeader[locationPosition];

pm.environment.set("sessionkey", sessionKey);

in Test tab while calling POst Create Session

Jim C
  • 3,957
  • 25
  • 85
  • 162
  • Does this answer your question? [How can I get last characters of a string](https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string) – Henke Feb 02 '21 at 13:20

1 Answers1

3

Add the following script in the first request test, it will set sessionkey value that you can use for later calls.

var locationHeader = postman.getResponseHeader("Location").split('/');

var sessionKey  = locationHeader[locationHeader.length-1];

pm.environment.set("sessionkey", sessionKey);
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Sorry, same error: "There was an error in evaluating the Pre-request Script: TypeError: postman.getResponseHeader is not a function". I will add a shot screen but it seems the postman version I am using doesn't have "postman.getResponseHeader" neither "pm.getResponseHeader". Do I have to install some plugin in Postman to have such function? I guess not. – Jim C Jul 26 '19 at 17:10
  • 1
    Actually your answer is 100% correct. I didn't get the point you point to use in test tab instead of Pre-request Script tab. – Jim C Jul 26 '19 at 20:07