How do you convert a Request via the Code button into script language compatible with Postman’s script language? I want to paste the request into a pre-request script.
i.e. I want the generated code to look like:
pm.sendRequest(“https://postman-echo.com/get ”, function (err, response) {
console.log(response.json());
});
None of the 20+ languages, including the 3 JavaScript versions, look like Postman Javascript.
Hey @BobDuffett ,
I think the Nodejs - Request
would be the closest at auto-creating what you need. It would give you something like this:
var request = require('request');
var options = {
'method': 'GET',
'url': 'https://postman-echo.com/get',
'headers': {
'test-header': '123345'
}
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
If you slightly changed it to this and added it to the pre-request, it would work:
var options = {
'url': 'https://postman-echo.com/get',
'header': {
'test-header': '123345'
}
};
pm.sendRequest(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.json());
});
More examples for the pm.sendRequest()
function can be found here:
example.md
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');
});
});
This file has been truncated. show original