Search code examples
validationtestingheaderpostmancode-snippets

Which snippet to use in Postman test to validate a part of a header value


Within the response of an API POST request the location is returned in the header as a key and a value build from 2 parts: URL path+unique id

Screenshot Postman header

I'd like to validate within the Postman test only the URL path of the value, because unique Id will be different after every new request.

Example of Location header value in the response:

api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses/5637454148

I'd like to validate only:

api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses/

So far i have:

    pm.response.to.have.header("Location");
    pm.response.to.be.header("Location", "api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/E000088/emailaddresses");

But this is not 100% correct


Solution

  • If you just wanted to check that the header contains part of the string you could use something like this:

    pm.test("Location Header", () => {
            pm.response.to.have.header("Location");
            pm.expect(pm.response.headers.get("Location")).to.include("api/v1/users/cbca75db-7d7d-4687-9aa0-c68f4rg46465/customers/EXXXXXXXXX/emailaddresses")
        })
    

    I don't know if your guid and id in the URL path would change so this is very much hardcoded and not dynanic.