Powershell curl double quotes
Asked Answered
B

3

9

I am trying to invoke a curl command in powershell and pass some JSON information.

Here is my command:

curl -X POST -u username:password -H "Content-Type: application/json" -d "{ "fields": { "project": { "key": "key" }, "summary": "summary", "description": "description - here", "type": { "name": "Task" }}}"

I was getting globbing errors and "unmatched braces" and host could not be resolved, etc.

Then I tried prefixing the double quotes in the string with the backtick character, but it could not recognize the - character in the description json field

thanks

EDIT 1:

When I wrote the curl command in a regular batch file, I used double quotes and no single quotes. Also, in the -d string, I escaped all the double quotes with \ and the command worked.

In this case, my curl is actually pointing to curl.exe. I specified the path, just didn't list it here. Also I tried adding single quotes around -d and I got:

curl: option -: is unknown curl: try 'curl --help' or 'curl --manual' for more information

Seems like it cannot recognize the - character in the JSON

Badalona answered 4/11, 2015 at 16:18 Comment(4)
1) Try enclosing the -d argument in single quotes instead of double 2) Make sure curl.exe is actually being called. If I remember correctly curl in powershell can be an alias for Invoke-WebRequestBuoyancy
hello, pls check the edit aboveBadalona
OK, might be the escaping then. In powershell the escape character is the backtick ` so maybe try using that on the quotes inside the jsonBuoyancy
@StevenPenny Seems to me that you want to clean all curl/powershell threads ^^. Does something like curl.exe -X POST "https://reqbin.com/echo/post/json" -H "Content-Type: application/json" -d (@{fields=@{project=@{key='key';summary='summary';description='description - here'; type=@{name='Task'}}}} | ConvertTo-Json -Depth 3 -Compress) would apply ?Iquique
R
11

Pipe the data into curl.exe, instead of trying to escape it.

$data = @{
    fields = @{
        project = @{
            key = "key"
        }
        summary = "summary"
        description = "description - here"
        type = @{
            name = "Task"
        }
    }
}

$data | ConvertTo-Json -Compress | curl.exe -X POST -u username:password -H "Content-Type: application/json" -d "@-"

curl.exe reads stdin if you use @- as your data parameter.

P.S.: I strongly suggest you use a proper data structure and ConvertTo-Json, as shown, instead of building the JSON string manually.

Rundell answered 4/11, 2015 at 16:49 Comment(7)
Can I expand variables in the json structure?. Like if I set $var="info", referencing $var would expand it?Badalona
Sure, that is a regular Powershell object. You can use any Powershell expression in it, you can use loops to build it, etc.Rundell
Also, in the $data variable, what are the @ characters for?Badalona
@{} is Powershell syntax for a hash table (associative array, key/value store, dictionary, object, whatever you may call it). Read up on it, for example here ss64.com/ps/syntax-hash-tables.html, here technet.microsoft.com/en-us/library/ee692803.aspx or in the official documentation.Rundell
Ok thanks, and what determines if the key and value should be declared as strings (double quotes) or not? Like you have name = "task", what if it was "name" = "task" or "name" = task? Or does it not matter when you convert to a JSON string?Badalona
The point of it all is that working with a real data structure that you convert to JSON in the end frees you from all character encoding and -escaping worries. You can build in a structured manner exactly what the server is supposed to see and convert it for transport only. JSON is a data persistence/transfer format, it is not really supposed to be manipulated directly, too much can go wrong.Rundell
It is not JSON until after it was processed by ConvertTo-Json. Before that point it's a Powershell data structure, obeying Powershell syntax rules. And as far as Powershell is concerned, quoted keys are possible, but completely optional. How those keys are supposed to look like in JSON is no longer your concern, the converter does the right thing for you automatically. The same goes for arrays (@()) and values like $true, $false or $null, etc. All that is taken care of.Rundell
M
5

Easy way (for simple testing):

curl -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}'
Multitude answered 21/9, 2020 at 20:36 Comment(1)
I think you should use curl.exe in PowerShell curl.exe -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}' because curl in PowersShell is Invoke-WebRequest, which differs from cURL in Linux.Inference
H
1

Easy way (for simple testing, with variables):

curl -X POST -H "Content-Type: application/json" -d "{ \`"field\`": \`"$value\`"}"
Hurlow answered 13/10, 2023 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.