Issue
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
Solution
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.
Answered By - Tomalak Answer Checked By - Mary Flores (WPSolving Volunteer)