I want to add query parameters for my request in postman through the pre-request script. How can I do this?
Asked
Active
Viewed 6,785 times
7
-
Could you expand on the reason for wanting to do this, please? – Danny Dainton Mar 31 '20 at 09:13
-
i want to add it in a collection's pre request script instead of manually adding for all APIs – Denise Mar 31 '20 at 11:07
2 Answers
10
Something basic like this could be used in the Collection Pre-request Script
:
pm.request.url.query.add({key: 'test', value: '1234'})

Danny Dainton
- 23,069
- 6
- 67
- 80
1
A more recent property exposed in Postman's pre-request scripts is the addQueryParams
method.
To add a single param:
pm.request.addQueryParams("id=123")
To add multiple params:
pm.request.addQueryParams([
"id=123",
"name=Homer",
"city=Springfield"
])
You can be super verbose as well:
pm.request.addQueryParams("id=123")
pm.request.addQueryParams("name=Homer")
pm.request.addQueryParams("city=Springfield")
An easy way to test out pre-request scripts is to use the Postman get url: https://postman-echo.com/get

Metro Smurf
- 37,266
- 20
- 108
- 140