Collection Run: Request and response bodies doesn't match

Guys, I have a problem. I have a request “Create a teacher”. In this request, in the request body, I use Dynamic variables:

{
    "username": "{{$randomUserName}}",
    "firstName": "{{$randomFirstName}}",
    "lastName": "{{$randomLastName}}",
    "email": "{{$randomEmail}}",
    "password": "{{$randomPassword}}",
    "role": "teacher"
}

In the Tests tab, I write a script to set these randomly generated username and password as collection variables:

const requestData = JSON.parse(request.data);
pm.collectionVariables.set("teacherUsername1", requestData.username);
pm.collectionVariables.set("teacherPassword1", requestData.password);

Here, I am addressing the request body since there is no password in the response.
Next, I use these variables teacherUsername1 and teacherPassword1 in my next request “Create a teacher token”:

{
    "username": "{{teacherUsername1}}",
    "password": "{{teacherPassword1}}"
}

Now the problem: when I run each request manually in order, everything works how it is supposed to work: username and password are randomly generated and saved as variables, and in the getting a token request, Postman uses these variables.
However!!! when I do a Collection run, I can see in the logs that the info in the request and response bodies of my “Create a teacher” request doesn’t match:
this a request body:

{
    "username": "Kameron.Roberts",
    "firstName": "Gabriel",
    "lastName": "Huel",
    "email": "Tremayne58@gmail.com",
    "password": "ucrTJRCkozxHpzk",
    "role": "teacher"
}

This is a response body:

{
    "id": 2286,
    "username": "Freddy42",
    "email": "Wade14@yahoo.com",
    "firstName": "Darrin",
    "lastName": "Kozey",
    "role": 2
}

The question is WHY? And how to solve this?

Hey @navigation-physici13 :wave:

The first question I need to ask is “Is everything saved?” when moving between the single requests and the Collection Runner, you need to ensure that everything is saved. If you have been editing the requests and not saving then before the run, this can cause issues.

You can also use the Autosave option to handle this action for you. This can enable this in the Settings Menu.

image

HI @danny-dainton !
Yeah, everything is saved. Cmd + S is my habbit afte VS code. I found a temporary solution. I use pre-req script in my “Create a teacher” request:

pm.collectionVariables.set("teacherUsername2", pm.variables.replaceIn("{{$randomUserName}}"));
pm.collectionVariables.set("teacherPassword2", pm.variables.replaceIn("{{$randomPassword}}"));
pm.collectionVariables.set("teacherFirstName", pm.variables.replaceIn("{{$randomFirstName}}"));
pm.collectionVariables.set("teacherLastName", pm.variables.replaceIn("{{$randomLastName}}"));
pm.collectionVariables.set("teacherEmail", pm.variables.replaceIn("{{$randomEmail}}"));

My request body looks like this:

{
    "username": "{{teacherUsername2}}",
    "firstName": "{{teacherFirstName}}",
    "lastName": "{{teacherLastName}}",
    "email": "{{teacherEmail}}",
    "password": "{{teacherPassword2}}",
    "role": "teacher"
}

And of course this part is deleted from the Tests tab:

// const requestData = JSON.parse(request.data);
// pm.collectionVariables.set("teacherUsername1", requestData.username);
// pm.collectionVariables.set("teacherPassword1", requestData.password);

Ok cool. :trophy:

This part was one that I was going to move onto next, If it wasn’t working now:

const requestData = JSON.parse(request.data);
pm.collectionVariables.set("teacherUsername1", requestData.username);
pm.collectionVariables.set("teacherPassword1", requestData.password);

The syntax didn’t look quite right to me :thinking:

Well I found part of the syntax here: https://community.postman.com/t/how-to-set-and-get-dynamic-environmental-variable/7482/4

That was me explaining the .replaceIn() part :smiley:

I was talking about the syntax you were using to get the username and password from the request.

If you have a JSON Request Payload like this:

{
    "username": "Danny",
    "password": "superSecret"
}

You could parse pm.request.body.raw to access the key/values from it in the Test section:

const requestData = JSON.parse(pm.request.body.raw);

pm.collectionVariables.set("teacherUsername1", requestData.username);
pm.collectionVariables.set("teacherPassword1", requestData.password);

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