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