Pre-request Script: Raw Body Data Display in Postman

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)

// 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

How looks good response (its work well)
image


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

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!

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:

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)

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.

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