How to get data from header 'location'

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
My issue is that I want to extract an url from header response (location): the response show 2 times the same url: exp: https://cam.uat.commercial.io/https:/cam.uat.commercial.io/v0/P221333/info

Details (like screenshots):

How to get only thr second part of the header response : https:/cam.uat.commercial.io/v0/P221333/info
Or only the id P221333 ?

How I found the problem:

Is there a solution ? Many thanks
I’ve already tried:

Hi @jadrissi

You could save the value returned in the header as a variable and then use slice to remove characters as needed.

Example;

let URL = 'https://cam.uat.commercial.io/https:/cam.uat.commercial.io/v0/P221333/info';
console.log(URL.slice(62, -5));

Output;


OR …

You could use a RegEx

Example:

let URL = 'https://cam.uat.commercial.io/https:/cam.uat.commercial.io/v0/P221333/info';
var array = new RegExp("/v0/(.*?)/info").exec(URL);
    if(array!=null && array.length>0 && array[1]!=null){
        //pm.globals.set("secret", ""+array[1]);
        console.log(array[1]);
    }

Output:

Hi @w4dd325 thank you so much I will try it and let you know

Thank you very much it works fine :ok_hand:

1 Like