2

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

6
  • "it does not seem to work" What does that mean? Commented Dec 23, 2017 at 2:45
  • When I put the script in a file it gives me the following validation error: Commented Dec 23, 2017 at 2:53
  • {"name":"VALIDATION_ERROR","message":"Unexpected end-of-input: expected close marker for OBJECT (from line:1, column:3","information_link" Commented Dec 23, 2017 at 2:53
  • Does Get-Content C:\test.json -Raw work? That loads the content into a single string instead of an array with one string per line. Commented Dec 23, 2017 at 2:54
  • No I still get the validation error Commented Dec 23, 2017 at 2:57

2 Answers 2

3

Try:

$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[.]

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

Comments

0

Remember curl in PoSH, is really this...

Get-Alias -Name curl

CommandType Name
----------- ----
Alias       curl -> Invoke-WebRequest 

... if you were not aware. The is another alias for Invoke-WebRequest as well.

If you want to use true curl in your PoSH session, you must use the full UNC to the executable (curl.exe) and that .exe extension is required or PoSH will use the alias above.

Get-Help -Name Invoke-WebRequest -Examples
Get-Help -Name Invoke-WebRequest -Full

So, for Invoke-WebRequest, the JSON must be in the body. Something like...

Invoke-WebRequest -uri "blahblahwhatever.com/api/uri" -Method POST -Body $JSON

So, virtually all the cmdlets have aliases, thus when we use commands we know that are really aliases, we must keep in mind what the real underlying cmdlet can and cannot do.

6 Comments

Note, that this answer, while correct at the time, is now obsolete. Most recent Window 10 and Windows Server 2019 have curl installed, and it is no longer an alias
Not true. WIn10 current / insider still has it. Get-CimInstance -ClassName [Microsoft Windows 10 Pro and Insider Preview 10.0.18363 / 10.0.19624], [PSVersion 5.1.18362.752 / 5.1.19624.1000 ], Get-Alias -Definition Invoke-WebRequest [Alias curl -> Invoke-WebRequest ] I don't have a WS 2019 up right now to check. To use the real curl on Win10, you must use the full name curl.exe, or PowerShell will default to the alias as per PowerShell command precedence.
I guess anybody reading this will have to check by themselves. I am getting Get-Alias -Name curl -> Get-Alias: This command cannot find a matching alias because an alias with the name 'curl' does not exist. and Get-Command curl -> Application curl.exe 7.55.1.0 C:\Windows\system32\curl.exe on Windows 10 Pro v.1909 (18363.900)
Yeppers I guess the will, because even, this... Get-Alias -Name curl CommandType Name Version Source ----------- ---- ------- ------ Alias curl -> Invoke-WebRequest, I get the alias results, and I just updated to the lastest inside build and v2004. It's still there.
it seems that it is the version of PowerShell that makes a difference; not Windows OS. Powershell 7 does not use alias any more. PS 5.1 does
|

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.