3

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?

Coldchain9
  • 1,373
  • 11
  • 31
  • 2
    Probably this is just a mistake when editing to post here, but there is a `'` missing at the end of the website string. – Jardel Lucca May 31 '23 at 20:13

1 Answers1

1

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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Worked like a charm. thank you for your help. I know Bash far better than Powershell. Very strange design that they went and used a backtick for an escape sequence instead of the classic ```\``` – Coldchain9 May 31 '23 at 19:59
  • 1
    Glad to hear it, @Coldchain9; my pleasure. As for why ``\`` wasn't chosen as the escape character: see the bottom section of [this answer](https://stackoverflow.com/a/74211220/45375). – mklement0 May 31 '23 at 20:04
  • Thanks for sharing bonus information! :) Additionally, this came to mind: What major changes would be necessary when using ```cmd``` vs ```powershell```? Seems like the pattern is different there as well. – Coldchain9 May 31 '23 at 20:08
  • 1
    @Coldchain9, `cmd.exe` and PowerShell are fundamentally different beasts. [This answer](https://stackoverflow.com/a/68794580/45375) compares the escape characters, string literal types, and variable-reference syntaxes between PowerShell, cmd.exe, and POSIX-compatible shells, and may be a starting point. – mklement0 May 31 '23 at 20:12