Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

POST Request with CSV String using Curl on Windows

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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).

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading