1

I've got a thermal scanner that I'm trying to send triggers to. I'm able to achieve desired results with postman, but I need a standalone script to achieve this so I've been working in powershell. The manufacturer documentation merely specifies a POST as application/json and provide the raw format to be sent in the body as such:

{
  "data": [
    {
      "output": "DO2",
      "mode": "ON",
      "duration": 5,
      "delay": false
    }
  ]
}

Like I said, I have no issues getting the desired result using postman, but I can't figure out how this needs to be formatted in powershell. Powershell isn't complaining about my code, but the device is not accepting the commands like it does from postman, probably because my code is garbage and the device isn't receiving the data correctly. The documentation says to input this data in the body so I have defined the body variable and simply call the variable when Invoke-Restmethod:

$body = ConvertTo-Json @(
  '{"data":[
    {
      "output": "DO2"
      "mode": "ON"
      "duration": "5"
      "delay": false
      }
    ]
    }'
)

any help would be appreciated and thanks in advance!

1 Answer 1

1

Use ConvertTo-Json on objects that need to be converted to json (JavaScript Object Notation), which basically is text describing objects in a structured way.

It looks like you already have the info for the body as valid json string, so in your case, use a Here-String for the json body:

$body = @'
{
  "data": [
    {
      "output": "DO2",
      "mode": "ON",
      "duration": 5,
      "delay": false
    }
  ]
}
'@ 

and use that in your Invoke-Restmethod call

Sign up to request clarification or add additional context in comments.

1 Comment

yessir this worked. I'm new to code in general and mainly do web coding at that so I figured I wasn't quite getting something right here. Thank you so much for your help I've been working on this for a day and a half!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.