Pre-Request Script for Bearer Token


I currently have a simple pre-request script to get a bearer token for my request. However, I keep getting this error every time I try to send the request

“There was an error in evaluating the Pre-request Script:Error: No data, empty input at 1:1 ^”

This is my pre-request script:

const tokenUrl = ‘’;
const clientId = ‘xxxxxx’;
const clientSecret = ‘xxxxxx’;

const getTokenRequest = {
method: ‘POST’,
url: tokenUrl,
body: {
mode: ‘formdata’,
formdata: [
{ key: ‘grant_type’, value: ‘client_credentials’ },
{ key: ‘client_id’, value: clientId },
{ key: ‘client_secret’, value: clientSecret }

pm.sendRequest(getTokenRequest, (err, response) => {
const jsonResponse = response.json();
const newAccessToken = jsonResponse.access_token;
pm.variables.set(‘access_token’, newAccessToken);

1 Like

Have a look at the request\response in the console log.

All requests including those triggered by sendRequest get logged.

Is it going through ok, are you getting any errors?

I suspect the request is not being authorised correct, and therefore the following line is erroring as it can’t parse the response.

const jsonResponse = response.json();

You can create this as a normal Post request in Postman while you are troubleshooting it, and then setup as a pre-request script after you get it working.

It’s easier to see the request\response that way.

Hi @stylianoswaterlogic

I altered your code slightly to work against the project I am currently testing at work and it works for me:

I have obviously xxxxx out the sensitive data, but you can see the first like in the log at the bottom.

I suggest you take @mdjones suggestion and build it as a normal Post request first, ensure that it works and you get a response, and then copy the info across to the pm.sendRequest() call.

Hope this helps.