postmanpostman-pre-request-script

How do I add a given header to all requests in the given collection or collection-level header


How do I add a given header to all requests in the given collection or collection-level header in PostMan?

Details (like screenshots): I have “x-apikey” header & a bunch of requests in the collection without this specific header.

How can I avoid manual work of adding header one by one to each request?

I imported OpenAPI spec & it does not contain the needed header. Hence, the need to add it to each request.


Solution

  • You can to do this in your Pre-request Scripts at the Collection level.

    pm.request.headers.add({key: 'x-apikey', value: 'token_value' })
    

    When the request is executed, it will add the header any the requests. This can also be done at the in all the element level Pre-request Scripts.

    https://learning.postman.com/docs/tests-and-scripts/write-scripts/postman-sandbox-api-reference/#script-with-request-data