I am doing API tests for my server. The scenario below:
Update a song (PUT method)
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.
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”.
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.
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
}
});
});
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.
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.
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?