Pre-request Scripts in Postman | The Exploratory

preview_player
Показать описание
You can use pre-request scripts in Postman to execute JavaScript before a request runs. By including code in the Pre-request Script tab for a request, collection, or folder, you can carry out pre-processing such as setting variable values, parameters, headers, and body data.

For an example of a collection making use of pre-request scripts, check out Twitter’s public workspace in the link above. They use pre-request scripts at the collection level to handle authentication.

For an advanced and practical use case using pre-request scripts, check out this blog post by Allen Helton on refreshing OAuth 2.0 tokens.

Presenter: Sean Keegan, Developer Advocate
Рекомендации по теме
Комментарии
Автор

Hi, thanks for the explanation on this! I do have one question regarding Pre Request scripts, is there a way to change the Environment with a Pre Request Script? For example if I'm running a folder which is running against Environment "ABC" but one request within that folder I want to run against Environment "DEF" is there code i can put into Post Request Script of that particular request to do that?

SRW
Автор

Thanks for the detailed explanation. Can we write a pre request script to upload an image file as binary in the Body of the request?
I have an api in which I upload the image binary in the body before sending the request. I'm trying to validate this through script. Could you please guide me how to write the script for this. Thank You.

siddharthau
Автор

If I heard you right, the pre-request for the collection will fire before any of the requests in the collection. Thinking of OAuth2, very useful but the API we are required to use is highly rate-limited for the getting of tokens; each is good for 15 minutes. Is there a way to save the token variable so that if I run, say 3 different requests one after the other, it only needs to get one token and can re-use it for the second and third requests, rather than requesting a new token every time?

johnquarto
Автор

Hi, i’m not sure is this the official postman channel, if so, it’s possible to improve the accessibility of postman for blind users that use the screen reader? Thanks

sergiu_s