# Pre-request Script: Raw Body Data Display in Postman

**URL:** <https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010>\
**Category:** Help Hub\
**Tags:** login, pre-request-script\
**Created:** [August 27, 2023, 11:10am UTC](https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010 "2023-08-27T11:10:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kartmun](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@kartmun](https://community.postman.com/u/kartmun)\
**Post date:** [August 27, 2023, 11:10am UTC](https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010/1 "2023-08-27T11:10:46Z")

</div>

Hello everyone, I am totally don’t have idea how to fix it

I’m encountering a peculiar issue while working with Pre-request Scripts in Postman. I’m trying to log raw request body data in the console for debugging purposes. When I run the request directly from Postman, I can see the JSON structure of the request body displayed correctly in the raw log. However, when I execute the same request using a Pre-request Script, I’m getting unexpected results in the raw log.

In the Pre-request Script, I’m creating a request body object and then using `JSON.stringify` to pass it as raw data in the POST request. The request works, but when I try to log the raw request body data in the console, I’m either getting `[object Object]` or just a single string, without the expected JSON structure.

Here’s an excerpt of my Pre-request Script: // … (other code)

```auto
// Create an object for the request
var requestBody = {
    userName: pm.environment.get("Default_Name"),
    password: pm.environment.get("Default_Password"),
    recaptchaResponse: null
};

// Log the raw request body data
console.log("Request body in JSON:", JSON.stringify(requestBody));

pm.sendRequest({
    // ... (request configuration)
    body: {
        mode: 'raw',
        raw: JSON.stringify(requestBody)
    }
}, function (authResponse) {
    // ... (handle the authentication response)
});

```

How looks bad response  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/3/a3eab3e55fc35c297a344045d58697d006b84a80.png)

How looks good response (its work well)  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/f/9f3c46b4a6a401f9f933344fbbfa5c87379225b5.png)

```auto

```

And have a third type of error when I try to give JSON structure inside of “raw”

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/6/6/666c9ce7816179425d9c390d38c308a18b46bae2.png)

I’ve noticed that the behavior is different when I run the request directly from Postman versus using the Pre-request Script. What could be causing this discrepancy, and how can I ensure that the raw request body data is logged correctly when using a Pre-request Script?

Thank you for your assistance and insights!

```auto

```

---

<div class="post-metadata">

**Author:** ![kartmun](https://avatars.discourse-cdn.com/v4/letter/k/ed8c4c/32.png) [@kartmun](https://community.postman.com/u/kartmun)\
**Post date:** [August 27, 2023, 11:50am UTC](https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010/2 "2023-08-27T11:50:06Z")

</div>

I think it is necessary to add why this is being done and what the idea is.

I want to make Test cases In the format of request lists in the folder with the role

Tests:

I. List item

1. Functionality  
a. Admin  
a.1. List of requests in order

To support such a structure, it means copying the parent request, I want to reduce the number of requests, first by removing the receipt of tokens (I have 2 of them, with 2 different requests) in Pre-requests Scripts, while I really like the idea of dynamic variables, and I try use them 100% (and everywhere and in any form)

Together with GPT, I was able to generate the following:

```auto
console.log("Pre-request STARTED");
pm.environment.unset("x_xrsf_token");
pm.environment.unset("token");

// Request for XSRF token
pm.sendRequest('https://dev/api/xsrftoken', (error, response) => {
    if (error) {
        console.log("Error getting XSRF token:", error);
    } else {
        const responseJson = response.json();
        const xtoken = responseJson.response.token;
        pm.environment.set("x_xrsf_token", xtoken);

        //console.log("XSRF token obtained:", xtoken);

        // Call the sign-in request within the callback
        signInRequest();
    }
});

// Function to send the sign-in request
function signInRequest() {
    var x_xrsf_token = pm.environment.get("x_xrsf_token"); // Get the x_xrsf_token from environment

    console.log("Using xrsf_token:", x_xrsf_token);

    // Create an object for the request
    var requestBody = {
        userName: pm.environment.get("Default_Name"),
        password: pm.environment.get("Default_Password"),
        recaptchaResponse: null
    };

    console.log("Request body in JSON:", JSON.stringify(requestBody));

    pm.sendRequest({
        url: 'https://dev/api/account/signin',
        method: 'POST',
        headers: {
            'accept': 'text/plain',
            'Content-Type': 'application/json-patch+json',
            'X-XSRF-TOKEN': x_xrsf_token
        },
        body: {
            mode: 'raw',
            raw: requestBody
        }
    }, function (authResponse) {
        // Handle the authentication response here
        console.log("Authentication response:", authResponse);
    });
}

```

What I expect is that in the request (Creating Notification), in Pre-requests scripts , there will be 2 requests , and filling them from environment variables, followed by the request with all tokens (1 in headers, another bearer)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 29, 2023, 10:02am UTC](https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010/3 "2023-08-29T10:02:29Z")

</div>

You are getting an “Access forbidden” error.

Check the request headers for the successful request, and ensure its the same for the sendRequest() code in the failing request.

As a starter for 10, I suspect the “text/plain” is incorrect for the accept header. However, check the headers for the successful request and just copy those.

I doubt its the request body, as that looks the same in both request. The failing request is showing minified JSON, but it is the same as the working request.

---

<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:** [November 27, 2023, 10:02am UTC](https://community.postman.com/t/pre-request-script-raw-body-data-display-in-postman/51010/4 "2023-11-27T10:02:54Z")

</div>

Closed on November 27, 2023, 10:02am UTC

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