How to re-use a Request (POST) inside a Pre-request Script

Hi Everyone, first thank you all in advance.

I have a few requests in a collection and one of them is a POST request that creates Clockings based on studentId and DeviceId:
Headers:


Body:

I am trying to use the following in the TESTS tab:
pm.environment.set(“creatClockingReq”, pm.request)

Then in the GET request, I want to run this POST request inside the Pre-request script to add some logic…
I am trying the following:
pm.sendRequest(pm.environment.get(“creatClockingReq”))

But I am getting a 500 error on the POST request when calling it from the pre-request, but once running it alone it works just fine Oo,
Would you know how to solve this issue or a better idea to achieve what I am trying to do?

I doubt that the variable creatClockingReq is in the correct format for sendRequest().

Look up the sendRequest() function to see what parameters it needs, and compare it to the variable.

I am also trying the following, but does not work, I do not get it.
It is the same code I am using in the POST request, the only difference is that I am trying to send it using the pm.sendRequest…

Can you expand the request details in the console to show what’s actually getting sent in that 500 error request.

Hey Guys, Thank you soo far for the help!

Here you go @danny-dainton

Do you get an error if you send those id values in the body, as hardcoded strings? As in, "1234".

I’m just thinking of things that are different here and it looks like those values are number but those would be stored as strings in an environment file. Does wrapping them in in parseInt(pm.environment.get('deviceId')) have an impact?

Can you compare this to the working request that you send in the GUI?

Check both in the Console Logs.

Is anything missing.

I don’t think there is anything wrong with your code, but the response is not returning the body, which is causing the latest error as it can’t parse an empty body.

The following is an similar request to Postman Echo for comparison.

const postRequest = {
    url: "https://postman-echo.com/post",
    method: 'POST',
    header: {
        'Content-Type': 'application/json'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(
            {
                "studentID": "ABC",
                "deviceID": 123
            }
        )
    }
};

pm.sendRequest(postRequest, function (err, res) {
    if (err) {
        console.log(err);
    } else {
        pm.test("Status code is 200", () => {
            pm.expect(res).to.have.status(200);
            let resJson = res.json();
            console.log(resJson);
        });
    }
});

Hey Danny, I have tried that and no luck still :frowning:

This is what the original POST request looks like.



When you send that request, it should show in the console log.

I would compare the request there, to the request sent by the pre-request script.

As Danny has mentioned, you might want to take a closer look at the request body. Are the values strings or integers?

I checked and the code matches each other, it is the same code.

The values in the body do not matter I can send it as a string or integer the backend will convert it anyway (as long as it is a number), it works either way in the original POST collection.

This one does not make any sense to me, to be honest…
Could it be related to the header values? For every single request to this API I need to pass the same 2 headers:

You can check the headers in both requests to see if there are any differences there.

500 errors are normally server side.

“The server has encountered a situation it does not know how to handle”.

Have you tried hardcoding the body values in the sendRequest, just as a test?

Actually looking back at some of your screen shots.

This one appears to be missing those request headers. Although you would expect a different status code if it was failing authentication elements.

Ok Guys, Thanks for the help, I just found the place where I was mistaken.

I just added this at the top of the pre-request and now it looks like I have access to those values, and indeed the values hardcoded there worked too.

pm.environment.set("studentId", Math.floor(Math.random() * 500) + 1);
pm.environment.set("deviceId", Math.floor(Math.random() * (2600 - 2570 + 1)) + 2570);

Not quite right, but it worked as hardcoded INT values,

Then I just had to add this one at the top and use your suggestion to parse the values:

pm.environment.set("studentId", Math.floor(Math.random() * 500) + 1);
pm.environment.set("deviceId", Math.floor(Math.random() * (2600 - 2570 + 1)) + 2570);

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