I want to add query parameters for my request in postman through the pre-request script. How can I do this?
How to add request parameters for a postman request in pre-script request
Asked Answered
Could you expand on the reason for wanting to do this, please? –
Od
i want to add it in a collection's pre request script instead of manually adding for all APIs –
Wilkens
Something basic like this could be used in the Collection Pre-request Script
:
pm.request.url.query.add({key: 'test', value: '1234'})
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
© 2022 - 2024 — McMap. All rights reserved.