Manipulate Header Output and Save as Variable

Relatively new to Postman and APIs in general. I am making decent progress, but the following is tripping me up.

I need to store the output of a header called “Link” as a variable, however it contains additional output that I do not need. I can successfully store the output as is with: pm.environment.set(“themeLink”, pm.response.headers.get(“Link”));

Sample header output below:

<https://10.83.10.111:443/transfer/ef55dc09-59f3-4371-a29e-8a3c37bdf995/upload.zip>;rel="upload:default";type="application/octet-stream"

How can I capture only https://10.83.10.111:443/transfer/ef55dc09-59f3-4371-a29e-8a3c37bdf995/upload.zip and store this as a variable?

Hey @mackov83 :wave:

Welcome to the Postman Community! :postman:

I think using some regex should help you to extract that value from the header string, you could try something like this:

const headerString = pm.response.headers.get('Link'),
    urlValue = headerString.match(/<([^>]+)>/);

pm.environment.set('themeLink', urlValue[1]);
1 Like

@danny-dainton, thanks very much for the help. That has done the trick!

I will need to spend some time learning regex now :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.