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
CodePudding user response:
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 stringSystem.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.