I\'ve been trying (and failing for hours) to convert this cURL script into PowerShell:
curl -X POST \\
https://example.net \\
-H \'Authorization: Bearer 1234
Your $Body
variable contains a hashtable (@{ ... }), so you must explicitly convert it to JSON with ConvertTo-Json
.
Additionally, because you're calling an external program, you must escape the "
chars. in the JSON string as \"
[1]:
$CurlArgument = '-X', 'POST',
'https://example.net',
'-H', 'Authorization: Bearer 1234567890',
'-H', 'Content-Type: application/json',
'-d',
(($Body | ConvertTo-Json) -replace '"', '\"')
[1] This additional escaping requirement is highly unfortunate, but to date it has been kept around for the sake of backward compatibility. This GitHub docs issue tells the whole story.