Can you dynamically set Headers for integration tests?

My question:
Is it possible to dynamically set headers during integration tests?

Meaning: request is made multiple times during tests so that we can test different header values for request failure or success.

I’ve already tried:
Pre-request scripts. Setting headers in request. Setting headers in collection.

Hi there, welcome to the community!

Pre-request scripts run before the request is called the first time, so that code would only execute once.

In your Tests code, you could try something like this. This example uses our “echo API” which will send back the headers in the response body so you can verify that the custom header called “custom-header” here was sent correctly.

pm.test("again, but with different headers", function() {
    let url = "https://postman-echo.com/put"
    let payload = {
        "greeting": "hello world"
    }
    pm.sendRequest({
        url: url,
        method: 'PUT',
        header: {
            "Custom-header": "hello u114101",
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify(payload)
        }
    }, (err, res) => {
        let response = res.json()
        pm.response.to.have.property("headers")
        console.log(pm.response.headers)
        pm.expect(response.headers).to.have.property("custom-header")
    });
})