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

Kingamere
  • 9,496
  • 23
  • 71
  • 110
  • 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` – arco444 Nov 04 '15 at 16:26
  • hello, pls check the edit above – Kingamere Nov 04 '15 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 – arco444 Nov 04 '15 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 ? – Zilog80 Apr 01 '21 at 14:25

2 Answers2

10

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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Can I expand variables in the json structure?. Like if I set $var="info", referencing $var would expand it? – Kingamere Nov 04 '15 at 16:56
  • 1
    Sure, that is a regular Powershell object. You can use any Powershell expression in it, you can use loops to build it, etc. – Tomalak Nov 04 '15 at 16:57
  • Also, in the `$data` variable, what are the `@` characters for? – Kingamere Nov 04 '15 at 18:48
  • `@{}` 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 http://ss64.com/ps/syntax-hash-tables.html, here https://technet.microsoft.com/en-us/library/ee692803.aspx or in the official documentation. – Tomalak Nov 04 '15 at 18:58
  • 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? – Kingamere Nov 04 '15 at 19:04
  • 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. – Tomalak Nov 04 '15 at 19:05
  • 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. – Tomalak Nov 04 '15 at 19:13
5

Easy way (for simple testing):

curl -X POST -H "Content-Type: application/json" -d '{ \"field\": \"value\"}'
Alexander Fadeev
  • 2,110
  • 14
  • 16
  • 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. – YGautomo Jul 12 '21 at 17:39