5

I have below string and want to extract the value of code. I used split function but which runs fine in postman but when i execute same in newman it gives error.

header1=https://debugger.com/ultradebugcode?code=EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb&state=ABC

I want to extract the value of code. which in this case is

EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb

the code i am using is

    var str= pm.response.headers.get('header1');
    var str1= str.split('code=', 2)[1];
    var code= str1.split('&', 2)[0]; // get the code

It worked fine in postman but why newman is giving error here?

Maddy
  • 674
  • 1
  • 7
  • 26
  • Isn't that a query param rather than a header? – Danny Dainton Oct 26 '19 at 14:13
  • No, this long string is being returned in the response header. The header name is "header1" – Maddy Oct 26 '19 at 14:17
  • That's not what you have added to your question to show this though - You've shown a URL with that value in the `code` value in a query param. What does this like like in Postman? Can you share an image please? – Danny Dainton Oct 26 '19 at 14:19
  • If the header has the key `header1` then `pm.response.headers.get('header1')` would give you the value of that. Not sure why you're doing all the rest of that stuff after? – Danny Dainton Oct 26 '19 at 14:23
  • 1
    I'm not sure if you're new to the site or not but please add these details to the question rather than the comments. – Danny Dainton Oct 26 '19 at 14:29

1 Answers1

4

This worked for me:

let str = pm.response.headers.get("header1").split("code=")[1]

console.log(str.split("&")[0])

enter image description here

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • Tried this too. It worked in postman but failed in newman. with this code too got below error in newman. – Maddy Oct 26 '19 at 15:12
  • What version of Newman are you using? Are you sure that the header is being returned in those requests? Have you tried logging the different parts of the code to the console to ensure its actually splitting _something_ - If its the same collection being used to call the same endpoints there isn't a reason why it wouldn't work the same. – Danny Dainton Oct 26 '19 at 15:47
  • newman version is 4.5.3. I tried to isolate the problem today. The API returns the header1 only when status code is 302. In postman it works fine without any problem. But in newman same collection when i execute and to isolate prob I executed only this request and saw in newman it returned 200 OK and hence header1 is not returned. Its kind of strange how something works fine in postman and doesn't work in newman – Maddy Oct 27 '19 at 23:18
  • Try it with this enabled `--ignore-redirects` – Danny Dainton Oct 28 '19 at 06:38
  • Danny you are life saviour. It worked. with --ignore-redirects. Can you please let me know why is it used? – Maddy Oct 28 '19 at 08:11
  • 1
    It's just auto following the redirects or the 3xx response series. There's a setting in the app to auto follow them in the app so you might not have seen the same behaviour. Check out the `Settings` options. – Danny Dainton Oct 28 '19 at 08:15