Turn complete PUT call into a script?

  • Introduce The Question:

I am trying to create an off-line script file from a command that includes tests and a pre-request script, to toggle a network-conneted light on and off. I used a pre-built command from another user here, and it works perfectly inside Postman itself.

The issue is I can’t work out how to export it to run offline, as a powershell script or anything else.

I’ve managed to get a simpler version of this code, that just sends an “ON” or “OFF” command, to work using the Postman’s Code Snippet function (Powershell:Restmethod), but I’m a complete n00b and so don’t know how to write a full script that does the tests and parses the response to send the correct command.

  • Additional Data

PUT 192.168.1.178:9123/elgato/lights

BODY:

{
    "lights": [
        {
            "on": {{light_on_updated}}
        }
    ]
}

PRE-REQUEST SCRIPT

// If collection variable is not already set, defined a default value
if(pm.collectionVariables.get("light_on") === undefined) {
    pm.collectionVariables.set("light_on", 0);
}

//Defining updated value
pm.variables.set("light_on_updated", +!pm.collectionVariables.get("light_on"));

TEST

// Testing request didn't failed
pm.test("Request didn't failed", function () {
    pm.response.to.have.status(200);
});

// Testing light has been turned on or off as expected
const lightOnLabels = ["off", "on"]; // off is 0 and on is 1
const lightOnExpected = pm.variables.get("light_on_updated"); // Retrieving local variable
const lightOnExpectedLabel = lightOnLabels[lightOnExpected]; // Using on value as index to get label

pm.test("Light has been turned " + lightOnExpectedLabel, function () {
    // Checking new on value matches the expected one
    const lightOn = pm.response.json().lights[0].on
    pm.expect(lightOn).to.eql(lightOnExpected);
    // Storing on value for next call
    pm.collectionVariables.set("light_on", lightOn);
});

If you want to script this in PowerShell, then this would be a question for a Microsoft forum.

If you just want to run the collection, then you might want to consider Newman which is a command line interface for Postman.

1 Like

Thanks - the CLI is just what I need!

Weirdly, though, it’s not working as expected. The command seems to run without issue (except for a “System cannnot find the path specified” note) but the device I’m sending the command to doesn’t respond.

Sending the command manually through Postman (incl on the Runner tab) works perfectly, however. Where’s the issue likely to be?

That sounds ominous. Path for what? (For the collection you are trying to run).

Troubleshooting Newman is the same as troubleshooting requests in Postman.

Use the console log and tests to ensure everything is working as expected. Newman should show what is logged to the console when you run the collection .

You should at least have a test checking for 200ok response, and I’m assuming that when the request is sent successfully, it produces some sort of response that you can also assert on.

1 Like

Thanks! I found the issue. It was how the CLI version was handling environment variables – they were persistent on the web, but of course not when running over the CLI…

Solved, thanks for all your help!

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