showing results for - "postman scripts send request programmatically"
Antonio
21 Jan 2017
1// Example with a plain string URL
2pm.sendRequest('https://postman-echo.com/get', (error, response) => {
3  if (error) {
4    console.log(error);
5  } else {
6  console.log(response);
7  }
8});
9
10// Example with a full-fledged request
11const postRequest = {
12  url: 'https://postman-echo.com/post',
13  method: 'POST',
14  header: {
15    'Content-Type': 'application/json',
16    'X-Foo': 'bar'
17  },
18  body: {
19    mode: 'raw',
20    raw: JSON.stringify({ key: 'this is json' })
21  }
22};
23pm.sendRequest(postRequest, (error, response) => {
24  console.log(error ? error : response.json());
25});
26
27// Example containing a test
28pm.sendRequest('https://postman-echo.com/get', (error, response) => {
29  if (error) {
30    console.log(error);
31  }
32
33  pm.test('response should be okay to process', () => {
34    pm.expect(error).to.equal(null);
35    pm.expect(response).to.have.property('code', 200);
36    pm.expect(response).to.have.property('status', 'OK');
37  });
38});