How to add request parameters for a postman request in pre-script request
Asked Answered
W

2

8

I want to add query parameters for my request in postman through the pre-request script. How can I do this?

Wilkens answered 31/3, 2020 at 9:5 Comment(2)
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 APIsWilkens
O
13

Something basic like this could be used in the Collection Pre-request Script:

pm.request.url.query.add({key: 'test', value: '1234'})
Od answered 31/3, 2020 at 14:4 Comment(0)
M
7

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

Miticide answered 1/5, 2023 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.