curl --data-binary in PowerShell
Asked Answered
E

2

8

If I have a curl command such as:

curl <url> \
  -H 'Content-Type: application/json' \
  -H 'API-Key: <key>' \
  --data-binary '{"blah":"blah {\n  blah2(accountId: id, integrations: {int1: {Vms: [{Id: id2, resourceGroups: [\"test1\", \"test2\", \"test3\"]}]}}) {\n    integrations {\n      id\n      name\n      service {\n        id\n        slug\n      }\n    }\n  }\n}\n", "variables":""}'

What would be the equivalent to --data-binary within Powershell? Some answer stated just running curl.exe, others mentioned to change the content-type. This does work as a shell script fine though. Just wondering if its possible to convert it to use Invoke-WebRequest within Powershell.

Enchain answered 2/10, 2018 at 18:49 Comment(4)
Use either Invoke-RestMethod or Invoke-WebRequest with the -InFile parameter.Lavinia
I'm not reading from a file though- just inline json being sent with --data-binaryEnchain
Will sending a JSON body work? You can use the -Body parameter.Lavinia
The -Body parameter seems to not care about being binary or not- works as expected, though it does require specific escape characters- thanks for the feedback!Enchain
E
-4

-Body parameter works with the payload

Enchain answered 2/10, 2018 at 21:3 Comment(1)
Please flesh this out in more detail to help future readers, including explaining what specific escape characters you had to use.Lurk
C
0

Use either Invoke-RestMethod or Invoke-WebRequest with -Body parameter but remember to convertto-json as the content-type is 'application/json'

example:

$body = @{"blah"="blah";"blah2"="blah2";}
$jsonBody = ConvertTo-Json $body
Invoke-RestMethod -Uri $uri -Method Post  -ContentType 'application/json' -Headers $Headers -Body $jsonBody
Chesson answered 30/9, 2023 at 3:7 Comment(0)
E
-4

-Body parameter works with the payload

Enchain answered 2/10, 2018 at 21:3 Comment(1)
Please flesh this out in more detail to help future readers, including explaining what specific escape characters you had to use.Lurk

© 2022 - 2024 — McMap. All rights reserved.