How to Generate Code Snippet that can be Pasted into Script Body?

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: