Skip to content

Instantly share code, notes, and snippets.

@gghughunishvili
Forked from sdnts/example.md
Last active March 28, 2024 17:10
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save gghughunishvili/5672a40eb0830f45839b056f2f517f14 to your computer and use it in GitHub Desktop.
Save gghughunishvili/5672a40eb0830f45839b056f2f517f14 to your computer and use it in GitHub Desktop.
Postman pm.sendRequest example

To send a request via the sandbox, you can use pm.sendRequest.

pm.test("Status code is 200", function () {
    pm.sendRequest('https://postman-echo.com/get', function (err, res) {
        pm.expect(err).to.not.be.ok;
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res).to.have.property('status', 'OK');
    });
});

Without additional options, this will sent a GET request to the URL specified. If you prefer to be more explicit, you can use the complete syntax:

pm.sendRequest({
    url: 'https://postman-echo.com/post',
    method: 'POST',
    header: 'headername1:value1',
    body: {
        mode: 'raw',
        raw: JSON.stringify({ key: "this is json" })
    }
}, function (err, res) {
    console.log(res);
});

More Example by @dmartinlozano:

pm.sendRequest({
      url:  pm.environment.get("OAUTH_URL")+"/uaa/oauth/token", 
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic Abcdefghijk=='
      },
      body: {
          mode: 'urlencoded',
          urlencoded: [
            {key: "grant_type", value: "password", disabled: false},
            {key: "username", value: pm.environment.get("OAUTH_USERNAME"), disabled: false},
            {key: "password", value: pm.environment.get("OAUTH_PASSWORD"), disabled: false}
        ]
      }
  }, function (err, res) {
        pm.globals.set("token", res.json().access_token);
  });
@craigiswayne
Copy link

Thank you!

@danipralea
Copy link

a small note:
instead of pm.environment.get("OAUTH_URL")+"/uaa/oauth/token"
you can have pm.environment.replaceIn("{{OAUTH_URL}}/uaa/oauth/token")

@aodennison
Copy link

'this will sent a GET request '
should read:
'this will send a GET request '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment