Restrict some HTTP Methods on Postman

Viewed 199

I need to restrict some of the HTTP Methods like PUT, POST and DELETE for my one of the environment. Is it possible in POSTMAN ?

This will help me in avoiding mistakes of doing POST,PUT or DELETE on my one of the environment.

2 Answers

Postman does not provide any in-built functionality like this. However, you can use pre-request scripts for this. Write this in your pre-request script of the API you want to limit certain request methods-

var request = pm.request;
if(request.method.includes("POST") || request.method.includes("PUT")){
    console.error("Inavlid request method");
    throw new Error("Invalid request method");
}

The drawback of this approach is that you need to copy-paste this in every API's pre-request script. If you want to bypass that, you can cache this entire code into a postman variable and just eval that variable in every API. Steps-

  • Create an environment variable in your postman with this name as "my-script" and value as-

() => { var request = pm.request;
if(request.method.includes("POST") || request.method.includes("PUT")){ console.error("Inavlid request method"); throw new Error("Invalid request method"); }}

  • Now just copy and paste this line in every pre-request script in your collection-

eval(pm.environment.get('my-script'))();

Related