How to dynamically change the request Method?

I have a scenario where I have to change the request method from POST to a GET, based on a given condition. How do I do that?

Scenario:

  1. Request method originally is a POST

  2. Read the condition1 from environment

  3. If condition1 === true then pm.request.method.update(“GET”)
    something like this.

  4. I only want to change the method, nothing else.

  5. similar to how you add header dynamically using pm.request.methods.add method.

  6. I don’t want to use a getRequest method I only want to update the method from POST to GET.

Thanks in Advance.

Hi @MrAutomationGuy, did you know about the fact that Postman let’s you define custom methods also?

And you can use that to your advantage by making the request method itself a variable.

You can type in the method as {{METHOD}} or anything else as you’d like.
Then in your pre-request script, you can check your conditions as you’d like and make use of local variable or any other variable scope as you wish to and change the request method accordingly.

Something like so:

2 Likes

omg, thank you @singhsivcan. Yes yes, that resolved my issue. Where is this documented? that’s why you are a leader :slight_smile: Thank you Sir.

Thanks to you I found the link too: Custom HTTP Methods == More Flexibility and Autonomy | Postman Blog

2 Likes

Glad that it helped you out! :smiley:

Docs: https://learning.postman.com/docs/postman/customizing-postman/
The original GitHub ticket: Custom HTTP Methods · Issue #166 · postmanlabs/postman-app-support · GitHub