I am using cURL on Windows by attempting to use two shells with varying results(Powershell, Git Bash).
I want to POST a CSV string to an API. I know this API works because I have tested it in Python, however I cannot get it to work in the shell.
Example:
Powershell (Curl Version 8.0.1)
C:\Windows\System32\curl.exe -k
"https://website.com" -X POST -H "Content-Type: application/octet-stream" --data-raw $"LATITUDE,LONGITUDE\n53.3737131,-1.4704939\n53.3742428,-1.4677477\n53.3745646,-1.467576\n53.3758092,-1.4665675\n"
Git Bash (Curl Version 7.80)
$ curl -k 'https://website.com' -X POST -H 'Content-Type: application/octet-stream' --data-raw
$'LATITUDE,LONGITUDE\n53.3737131,-1.4704939\n53.3742428,-1.4677477\n53.3745646,-1.467576\n53.3758092,-1.4665675\n'
The Git Bash solution works.
The API is telling me I am missing the LATITUDE column in my CSV string. Without going into specifics on the API (since I know it works) is there something I could be missing in my curl request that is causing this CSV string to be malformed when sent?
There are no $"..." strings in PowerShell[1] (you seem to be thinking of an analog to ANSI C-quoted strings, $'...', supported in some POSIX-compatible shells such as Bash).
Simply use "...", i.e. an expandable (double-quoted) string ("..."), in which you can use escape sequences such as `n to represent a newline (the equivalent of \n inside $'...' in Bash, for instance - note that PowerShell's escape character is `, the so-called backtick)[2].
However, note that inside "..." strings $ is also special - it enables embedding variable reference and subexpressions to be expanded (interpolated) - so any $ chars. to be treated verbatim must be escaped as `$.
If you need neither escape sequences nor interpolation, use '...', i.e. verbatim (single-quoted) strings instead (their only escaping requirement is that embedded ' must be escaped as '').
Therefore:
# From PowerShell.
# Note the `n instances in lieu of \n in the final, "..."-enclosed argument.
curl.exe -k 'https://website.com' -X POST -H 'Content-Type: application/octet-stream' `
--data-raw "LATITUDE,LONGITUDE`n53.3737131,-1.4704939`n53.3742428,-1.4677477`n53.3745646,-1.467576`n53.3758092,-1.4665675`n"
[1] In a command argument, the $ is simply prepended verbatim to whatever the subsequent "..." string expands; e.g., Write-Output $"foo" outputs verbatim $foo. The same applies analogously to following $ with '...' (single quotes)
[2] As for why PowerShell didn't choose \ as the escape character: Since \ is the path separator on Windows (and PowerShell has Windows-only roots), this would have made specifying file-system paths quite cumbersome, due to the resulting need to escape such - verbatim - separators (e.g. C:\\Foo\\Bar to refer to C:\Foo\Bar). See the bottom section of this answer for a detailed discussion.
This answer compares the escape characters, string literal types, and variable-reference syntaxes between PowerShell, cmd.exe, and POSIX-compatible shells.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With