英文:
POST Request with CSV String using Curl on Windows
问题
我正在使用cURL在Windows上尝试使用两个不同的shell(Powershell、Git Bash),结果各不相同。
我想要将一个CSV字符串POST到一个API。我知道这个API有效,因为我已经在Python中进行了测试,但是在shell中无法使其正常工作。
示例:
Powershell(Curl版本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版本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'
Git Bash的解决方案有效。
API告诉我,在我的CSV字符串中缺少LATITUDE列。除了API的具体细节(因为我知道它有效)外,是否有可能在我的curl请求中漏掉了什么,导致这个CSV字符串在发送时格式不正确?
英文:
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?
答案1
得分: 1
在 PowerShell 中没有 $"..."
字符串[1](你似乎在想到 ANSI C-quoted 字符串,$'...'
,它在一些与 POSIX 兼容的 shell 中受支持,比如 Bash)。
只需使用 "..."
,即可展开(双引号)字符串 ("..."
),其中可以使用转义序列如 `n
来表示 换行(与 Bash 中 $'...'
内的 \n
等效 - 请注意 PowerShell 的转义字符是 `
,即所谓的反引号)[2]。
但是,请注意,在 "..."
字符串_内部_ $
也是特殊的 - 它允许嵌入变量引用和子表达式进行展开(插值) - 因此必须将任何 $
字符进行转义,如 `$
。
如果既不需要转义序列也不需要插值,请使用 '...''
,即字面量(单引号)字符串(它们唯一的转义要求是将嵌入的 ''
转义为 '''
)。
因此:
# 来自 PowerShell。
# 注意最终的,用 "..." 括起来的参数中的 `n 实例代替 Bash 中的 \n。
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"
<sup>[1] 在命令参数中,$
简单地 逐字前置 到后续 "..."
字符串展开的任何内容;例如,Write-Output $"foo"
会逐字输出 $foo
。类似地,将 $
与 '...'
(单引号)相随后也会类似地处理</sup>
<sup>[2] 关于为什么 PowerShell 没有选择 \
作为转义字符:因为 \
是 Windows 上的 路径分隔符(而 PowerShell 只有 Windows 特定的根目录),这会使得指定文件系统路径变得非常繁琐,因为必须对这样的 - 逐字 - 分隔符进行转义(例如,C:\\Foo\\Bar
用于引用 C:\Foo\Bar
)。详细讨论请参见 此答案 中的底部部分。
此答案 比较了 PowerShell、cmd.exe
和与 POSIX 兼容的 shell 之间的转义字符、字符串字面量类型和变量引用语法。
</sup>
英文:
<!-- language-all: sh -->
There are no $"..."
strings in PowerShell<sup>[1]</sup> (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)<sup>[2]</sup>.
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"
<sup>[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)</sup>
<sup>[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.
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论