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
How looks good response (its work well)
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!