Response body not taking data from request body

const x = pm.globals.get(“vse”);

function processItem(item) {
pm.sendRequest({
url: “…”,
method: “POST”,
header: {
“authorization”: “…”
},
body:
JSON.stringify({
“name”: item.naziv,
“price”: item.prodajnaCena1DDV,
“stock_quantity”: item.zaloga,
“manage_stock”: true,
})
}, function (err, response) {});
}

x.forEach((item, index) => {
setTimeout(() => {
processItem(item);
}, index * 350);
});

if i run this, its request body is correct but response body doesn’t take any data from it like its empty. if i put the exact same request body into a new http request not in tests, it works. why is that?

Please use the preformatted text option when using the editor. It stops all of your code from aligning to the left.

This seems to work for me with similar code against Postman Echo.

const x = [{ "naziv": "name1", "prodajnaCena1DDV": "price1", "zaloga": "quantity1" }, { "naziv": "name2", "prodajnaCena1DDV": "price2", "zaloga": "quantity2" }]

function processItem(item) {
    pm.sendRequest({
        url: "https://postman-echo.com/post",
        method: "POST",
        body:
            JSON.stringify({
                "name": item.naziv,
                "price": item.prodajnaCena1DDV,
                "stock_quantity": item.zaloga,
                "manage_stock": true,
            })
    }, function (err, res) {
        if (err) {
            console.log(err);
        } else {
            pm.test("Status code is 200", () => {
                pm.expect(res).to.have.status(200);
                let response = res.json();
                // console.log(response);
            });
        }
    });
}

x.forEach((item, index) => {
    setTimeout(() => {
        processItem(item);
    }, index * 350);
});

I suspect that you need to JSON.parse() your global variable. Otherwise it will just be treated as a string.

const x = JSON.parse(pm.globals.get("vse"));

Although does this really need to be a global variable available to all of your collections in Postman. Wouldn’t a collection or environment variable be a more suitable scope.

This happens if i run it from the test script.

And this happens if I run it from not a script.

The data in the screenshots is the same. The only difference is that the first is showing the JSON as minified, and the other is beautified. But the JSON has the same key/value pairs.

If you take a close look at the response body of both, you will see the first image takes default text and doesn’t take data from request body but the second one does. You see where it says “name”: … or “price”: … in the response body.

The response is controlled by the API.

Both of those requests are sending the same body, so the issue is somewhere else.

Have a look at the headers for both requests, are there any differences there. Is the correct content-type being sent?

I managed to fix the issue. I changed the body mode to formdata and it works now. Thanks for your help anyway.

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