-2

I am trying to make a post request in powershell using curl. But I seem to be unfortunately getting this error.

I have tried removing spaces here and there, and googling the problem but have not found a solution.

curl.exe -X 'POST' -H @{'Content-Type'='application/json'; 'accept'='application/json'}  -d \"{\"name\":\"test3\", \"auto_init\":true, \"default_branch\": \"master\", \"description\": \"My Test\", \"gitignores\": \"Vim\", \"issue_labels\":\"Default\", \"license\": \"DOC\", \"name\":\"test2\", \"private\":false, \"readme\":\"Default\",\"template\":false,\"trust_model\":\"default\"}\" http://localhost:3000/api/v1/user/repos?access_token=c11ceb97fa594a7e6c4b5519e4327908be3274b9
ebrahim
  • 105
  • 1
  • 7
  • Apply [the stop-parsing token](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parsing?view=powershell-7#the-stop-parsing-token) `--%`? Pass headers as a string. BTW, why do you use escaped ```\"``` at starting and ending positions in `-d` data? – JosefZ Dec 24 '21 at 22:17
  • @JosefZ Yah i guess i could delete those and it would still work – ebrahim Dec 24 '21 at 22:31

1 Answers1

1
  • Re -H:

    • curl.exe is an external program, which means that you cannot meaningfully pass a PowerShell hashtable (@{ ... }) as an argument, because it will (uselessly) be passed as literal string System.Collections.Hashtable.

    • Instead, pass strings, as multiple -H options, each in the form '<name>: <value>'

  • Re -d:

    • PowerShell's escape character is ` (the so-called backtick), not \.

    • Since your argument is to be passed verbatim (contains no variable references to be interpolated), use a verbatim (single-quoted) string ('...').

    • However: The sad reality as of PowerShell 7.2 is that an extra, manual layer of \-escaping of embedded " characters is required in arguments passed to external programs. This may get fixed in a future version, which may require opt-in. See this answer to the linked duplicate for details.

To put it all together:

curl.exe -H 'Content-Type: application/json' -H 'accept: application/json' -d '{\"name\":\"test3\", \"auto_init\":true, \"default_branch\": \"master\", \"description\": \"My Test\", \"gitignores\": \"Vim\", \"issue_labels\":\"Default\", \"license\": \"DOC\", \"name\":\"test2\", \"private\":false, \"readme\":\"Default\",\"template\":false,\"trust_model\":\"default\"}' 'http://localhost:3000/api/v1/user/repos?access_token=c11ceb97fa594a7e6c4b5519e4327908be3274b9'

Note: I've omitted -X 'POST', because, as Daniel Stenberg notes, a POST request is implied when you use the -d option.

mklement0
  • 382,024
  • 64
  • 607
  • 775