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

4
  • 1
    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-WebRequest Commented Nov 4, 2015 at 16:26
  • hello, pls check the edit above Commented Nov 4, 2015 at 16:29
  • 1
    OK, might be the escaping then. In powershell the escape character is the backtick ` so maybe try using that on the quotes inside the json Commented Nov 4, 2015 at 16:43
  • @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 ? Commented Apr 1, 2021 at 14:25

3 Answers 3

12

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.

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

7 Comments

Can I expand variables in the json structure?. Like if I set $var="info", referencing $var would expand it?
Sure, that is a regular Powershell object. You can use any Powershell expression in it, you can use loops to build it, etc.
Also, in the $data variable, what are the @ characters for?
@{} 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.
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?
|
5

Easy way (for simple testing):

curl -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}'

1 Comment

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.
1

Easy way (for simple testing, with variables):

curl -X POST -H "Content-Type: application/json" -d "{ \`"field\`": \`"$value\`"}"

Comments

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.