Pre-request scripts not working for me

I added some JavaScript in the pre-request script tab for a collection to add some headers but none of them are getting executed.
Here is the text I added:
pm.request.headers.add({key: “Authorization”, value: “Basic xxxxxxxxxx” })

here, xxxx is the key value.

I looked at the console logs and this header is not showing up there, I also added a console.log(“Hello”) that is also not showing up in the logs.

Would really appreciate some help on this.

Hi @ritvik-saras

Not really sure why it doesn’t work for you, it works for me;

image

If it’s still not working could you screenshot what you are sending / seeing etc?

is this at a collection level?


This is how my request headers are

That was tested at both collection level and HTTP request level, both work.

Here is the output from setting at the collection level and running the collection runner.

Note that, if you set it at the collection level, if you run the HTTP request individually, you will not trigger the pre-req script at the collection level, you need to run via the collection runner for that you happen.

Hope that helps.

I ran into this issue. I just want to set “Accept” header for the whole collection so I don’t have to set the header for each request. What would the right way to do it?

Hey @lunar-module-geosci2 :waving_hand:

Welcome to the Postman Community :postman:

You can set that via the pre-request script at the Collection level as mentioned in the previous posts.

This would be applied to each request in the Collection. You can wrap some logic around that if you need that to target a specific request.

Using Agent Mode will help you do this :heart: