GVal GVal - 2 months ago 51
JSON Question

How to put json script in a file to use in PowerShell using cURL

I'm trying to pass a json script to curl using powershell. It seems to work if I hard code it right into a variable escaping the double quotes with backslash, but I want to pass the json in a file. I tried putting the json script in a separate json file and then use get-content and throw it in a variable, but it does not seem to work I get a "Validation_Error". Any suggestions

This Works

enter image description here

But I want to do something like this

$json = Get-Content C:\test.json

curl -v -X POST https://api.paypal.com/v1/invoicing/invoices/ \ -H "Content-Type:application/json" \ -H "Authorization: Bearer $token" \ -d $json

Answer Source


$json = Get-Content C:\test.json -Raw

By default Get-Content loads into an array of strings. The -Raw flag forces it to load into a single string.

You should also be able to do this and tell curl to read the file itself:

curl -v -X POST https://[...] -d @test.json

From the man page:

If you start the data with the letter @, the rest should be a file name to read the data from[.]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download