JSONError: No data, empty input at 1:1 ^

I am doing API tests for my server. The scenario below:

  1. Update a song (PUT method)
  2. After update a song, verify if the song has been updated with GET method.
    The problem is when I call response.json(), it seems like Postman throw erros as shown in the title.
    I am pretty sure my server returns correct data. I also checked at the console and it has the response body that I am looking for.

Details:

  1. This is the part where I get the problem
pm.test('after updating the song', () => {
    const songId = pm.environment.get("updateSongId");

    const getRequest = {
        url: `http://localhost:9000/songs/${songId}`,
        method: 'GET'
    };
    pm.sendRequest(getRequest, (error, response) => {
        if (!error) {
            pm.test("Updated songs must match", () => {
                const responseJson = response.json();
                pm.expect(responseJson.data).to.be.an('object');
                const song = responseJson.data;
                const songShouldBe = JSON.parse(pm.environment.get("updatedSongId"))
                songShouldBe.id = songId;
                pm.expect(song).to.deep.equal(songShouldBe)
            });
        } else {
            console.log(`error: ${error}`)
        }
    });
});
  1. The Postman Console

I can guarantee that the variable and the data related to updateSongId exists.

I’ve already tried:

  1. Manually create new request and the data exist.

  2. I have a similar GET request with call to pm.response.json() and Postman seems to parse them correctly even the response body in the console has Carriage Return at the end of the body. The environment variable I put in songId is “UIOlkjhB4”.

Thank you.

It looks like you have a carriage return on the end of the JSON which means its not returning valid JSON and the attempt to PARSE will fail.

Hi @michaelderekjones . Thanks for your reply. According to your point, I have updated my post where I have similar GET request with call to pm.response.json() and Postman seems to parse them correctly. It’s on “I’ve already tried:” section. Please check it.

Thank you.

Console log the “response” variable before you try and parse it.

What does it show in the console?

I am not really sure if this is what you want to see…

This is the console window on GET request:

and this is the console window on PUT (update) request:

Ok, the data is hidden in the stream, but isn’t a lot of use until its parsed.

So you can’t compare the two requests which I thought you might be able to do.

I’m still thinking that the new line is messing up the PARSE for the SendRequest. (It’s not a carriage return). I can’t mimic that, so I can’t test parsing it to see what would happen.

Not sure why it works if you send it through the main GUI instead.

I can’t see anything particularly wrong with your code. However, the following is an example you could use as a template. Just in case, there is something I’m missing.

pm.test("sendRequest test", function () {
    pm.sendRequest({
        url: 'https://postman-echo.com/get?test1=ABC&test2=DEF',
        method: 'GET',
    }, function (err, res) {
        if (err) {
            console.log(err);
        } else {
            pm.test("Status code is 200", () => {
                pm.expect(res).to.have.status(200);
            });
            let resJson = res.json();
            // console.log(resJson); 
            console.log(resJson.args); // Postman echos what was sent to the args element in the response
        }
    });
});

Hi, sorry for the late reply.

The given code works. Wrapping the last three lines in pm.test also works. The response body, though, it is pretty-printed and does not have newline ending on it.

About the new line is messing the parse for sendRequest, I try to make the server to not to put newline at the end of the JSON but the error persists.

I don’t think that the newline is messing up the JSON parsing. Given the third screenshot (from the start of the topic) parses the body correctly even it has newline in it.

My god… this is getting weird.

That looks like valid JSON.

What is the content type in your response headers?

Does it include “application/json”?

Are you sure its the response.json() line that is causing the failure and not the JSON.parse line that is also in the same test?

Console log the response.json() just after it set to check this or remark out the JSON.parse line.

As the request to Postman Echo works, then it would suggest on paper that its an issue with the API.

However, I would use the working code against Postman Echo and just change the URL to your endpoint and just console log the result with no other code. Does that work or also fail the parse?

You are actually correct that the JSON.parse line actually makes the test fail. The environment value was empty.

After looking at the environment menu, I accidentally cleared the current value field :joy: oh I feel embarrased now.

Thank you @michaelderekjones for pointing that out.

1 Like

No problem. Glad its working now.

Tip of the day: Console log everything.

1 Like