# Collection Run: Request and response bodies doesn't match

**URL:** <https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454>\
**Category:** Help Hub\
**Tags:** variables, collection-runner\
**Created:** [September 6, 2023, 5:00pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454 "2023-09-06T17:00:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![navigation-physici13](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@navigation-physici13](https://community.postman.com/u/navigation-physici13)\
**Post date:** [September 6, 2023, 5:00pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/1 "2023-09-06T17:00:14Z")

</div>

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

```auto
{
    "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:

```auto
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”:

```auto
{
    "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:

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

```

This is a response body:

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

```

The question is WHY? And how to solve this?

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 7, 2023, 11:37am UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/2 "2023-09-07T11:37:43Z")

</div>

Hey @navigation-physici13 👋

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](https://us1.discourse-cdn.com/getpostman/original/3X/6/2/6258876e71ad6f65028ed3c3365e09d48d795f72.png)

---

<div class="post-metadata">

**Author:** ![navigation-physici13](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@navigation-physici13](https://community.postman.com/u/navigation-physici13)\
**Post date:** [September 7, 2023, 12:03pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/3 "2023-09-07T12:03:14Z")

</div>

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:

```auto
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:

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 7, 2023, 12:07pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/4 "2023-09-07T12:07:45Z")

</div>

Ok cool. 🏆

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

```javascript
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 🤔

---

<div class="post-metadata">

**Author:** ![navigation-physici13](https://avatars.discourse-cdn.com/v4/letter/n/5f9b8f/32.png) [@navigation-physici13](https://community.postman.com/u/navigation-physici13)\
**Post date:** [September 7, 2023, 12:21pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/5 "2023-09-07T12:21:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 7, 2023, 1:10pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/6 "2023-09-07T13:10:58Z")

</div>

That was me explaining the `.replaceIn()` part 😃

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:

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

```

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

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

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

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [September 10, 2023, 1:11pm UTC](https://community.postman.com/t/collection-run-request-and-response-bodies-doesnt-match/51454/7 "2023-09-10T13:11:02Z")

</div>

Closed on September 10, 2023, 1:11pm UTC

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