15 days of Postman for tester - Day 07: Wrong Json format in the response body of https://postman-echo.com/post

I stumbled on a problem after submitting the collection for day 7
Below was what I did
After I submitted my collection, the 8th assertion was failed
image
And in the test script below, the echoRequest.event was undefined


My ā€œecho the userā€ API

as you can see in the console, the value of the ā€œdataā€ field is not JSON but a String!
However, the 8th assertion in the submit collection API is checking for this particular string ā€œpm.response.jsonā€ in the test script of the ā€œecho the userā€ API, otherwise, I could just use the pm.repsonse.text() method instead.
After doing some research on the internet and ask the chat GPT, Iā€™m not entirely sure if the failure of the 8th assertion is the wrong formatted json body or not. So, please feel free to share your insights :v

P/s: I think this issue has just occurred recently. I vividly remembered the response body of the echo API was neat and tidy when I did the day 5 challenge

This is using Postman echo, and it should be returning an object, not a string.

What does your body look like as Postman echo just echoā€™s the body back under the data element.

I get this from time to time when using Postman Echo, where the body looks fine but it keeps returning a string.

Sometimes I have to paste the body into notepad to remove any potential extra formatting, and then paste it back in which can fix the issue. Make sure all of the quotes are normal double quotes, etc as that can also affect it.

For this particular challenge, the variables in the body need to be within quotes like following.

  "name": "{{name}}",

Thanks for your response, but I donā€™t think it is the case. Below is my echo requestā€™s body


I donā€™t have any problems with the challenge, the problem is lying within the test script of the submit collection API. For some reasons, the ā€œeventā€ variable of the below block of code is undefined. I wonder if the reason is due to the incorrect formatted of the echo APIā€™s response body

pm.test(ā€œEcho test addedā€, () => {
let echoRequest = folder.item.find(req => { return req.name === ā€œecho the userā€})
console.log(echoRequest)
console.log(echoRequest.event)
let event = echoRequest.event.find(e => {return e.listen === ā€œtestā€})
pm.expect(event.script.exec.toString(), ā€˜check parse objectā€™).contains(ā€œpm.response.jsonā€)
pass += 1
})

Put the variables in the body in quotes, and then see if it returns an object.

{
  "name": "{{name}}",
  "email": "{{email}}",
  "uuid": "{{uuid}}"
}
1 Like

Could you please share the test script of the ā€œecho the userā€ folder?
I got stuck because my test case ā€œCollection variables setā€ failed.

My scripts are as below:
1. GET Canadian user
Param key: nat - value: CA
Tests:
//set collection variables

let responseJsonData = pm.response.json();
console.log(responseJsonData);
pm.collectionVariables.set(ā€œuserdataā€, responseJsonData);

2. POST echo the user
*Pre-request scripts:

let userdata = pm.collectionVariables.get(ā€œuserdataā€).results[0];
//set name variable

let fname = userdata.name.first;
let lname = userdata.name.last;
let fullname = Object.values([fname,lname]).join(" ");

pm.collectionVariables.set(ā€œnameā€, fullname);

//set email variable

let email = userdata.email;
pm.collectionVariables.set(ā€œemailā€, email);

//set uuid variable

let uuid = userdata.login.uuid
pm.collectionVariables.set(ā€œuuidā€, uuid);

Body:
{
ā€œnameā€: ā€œ{{name}}ā€,
ā€œemailā€: ā€œ{{email}}ā€,
ā€œuuidā€: ā€œ{{uuid}}ā€
};

Test submit collection failure:

Hope to hear from you <3

@phthanhxuan

First of all, this should be its own question. Rather than hi-jacking someone elseā€™s question (which is not related apart from its the same challenge).

I can only recommend to re-read the instructions. These challenges arenā€™t meant to catch you out, and telling you the answers defeats the main purpose of the challenges\training.

Take a step back and think about the error message.

Itā€™s expecting a variable in the GET request called ā€˜nameā€™.

You are meant to set the collection variables for name, email and UUID from the GET request response as individual elements in the tests tab.

Youā€™ve set an collection variable called ā€˜userdataā€™ using the full response which you then pull apart in the pre-request script for the POST request. You donā€™t need to do this in the pre-request script. Just set the collection variables for the three elements from the GET request.

@annq00

Iā€™m not sure that the formatting of the body would break the test. But is something that should be corrected as it should be returning an object.

image

Have a look at the response when you submit the collection.

Try and find the item object for ā€œecho the userā€.

The beginning of it should look something like the following. The event is related to the tests tab, not the body.

{
    "name": "echo the user",
    "event": [
        {
            "listen": "test",
            "script": {
                "id": "64ef265a-8115-4690-a957-e40af4f6ee6a",
                "exec": [
                    "const response = pm.response.json().data\r",
                    "\r",
                    "\r",
                    "pm.test(`Returns correct email: ${pm.collectionVariables.get('email')}`, function () {\r",
                    "    pm.expect(response.email).to.eql(pm.collectionVariables.get('email'));\r",
                    "})"
                ],
                "type": "text/javascript"
            }
        }
    ],

It should also match up with your console logs.

console.log(echoRequest)

It does on face value look like the event element is not appearing in the collection. You should be able to console log it as you have done.

@michaelderekjones

Dear Mr. Mike,

I apologize for my irrelevant question on this topic.

I misunderstood the instructions. I have followed your instruction and successfully redone it correctly.

Thank you so much for your reply!!

Dude, after I tried to submit my collection again, for some unknown reasons all of 8 assertions passed !!?
I have not changed anything at all since I posted this topic.
About the response of my ā€œecho the userā€ API, itā€™s still the same. The value of ā€œdataā€ is a string with \r\n all over the place

Now when I check the echoRequest and echoRequest.event with console.log, the result of echoRequest.event is no longer undefined which is of course because the test no longer fails

I literally have not changed anything at all and suddenly it works. Still confused as hell.
Anyway, thanks a lot for trying to help me out. Have a nice day.

Fair enough. Glad its passing now.

in relation to data showing a string instead of an object, you still havenā€™t updated the body to wrap the variables in quotes.

{
  "name": "{{randomUserName}}",
  "email": "{{randomEmail}}",
  "uuid": "{{randomUuid}}"
}

wow, it takes me a while to realize what you have been trying to tell me to do.
I did missed those quotes in my body. It worked well in the day 5 challenge without the quotes so I did not think twice about it. Btw I actually thought that you were referring to the double curly brackets since quote could be translated to that in my mother tongue. Silly me :v
Now itā€™s all clear. Thanks a ton, man xD

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.