卷曲 <- 功能

Jür*_*aul 7 command-line curl

$ cat file | curl -F 'sprunge=<-' http://sprunge.us
Run Code Online (Sandbox Code Playgroud)

因此,输出echo作为 POST 参数传递给 cURL。这是特定于 cURL 的功能吗?

小智 8

-通常用于表示标准输入,<通常用于表示从文件重定向。我相信这些语法来自早期的 shell。它们一起意味着接收标准输入并将其发送/重定向到其他地方。语法几乎是自然的。

查看 cURL修订历史,该<语法是在 2000 年中期添加到 cURL 中的。添加此功能的修订版可作为 Git commit 使用5b7a5046e6

从变更日志中,

Torsten Foertsch <torsten.foertsch at gmx.net> brought a set of fixes for
the rfc1867 form posts. He introduced 'name=<file' which brings a means to
suuply very large text chunks read from the given file name. It differs from
'name=@file' in the way that this latter thing is marked in the uploaded
contents as a file upload, while the first is just text (as in a input or
textarea field). Torsten also corrected a bug that would happen if you used
%s or similar in a -F file name.
Run Code Online (Sandbox Code Playgroud)

没有提及此功能的灵感或起源。

@-语法出现在我能找到的最早版本的源代码中的 cURL 中。从 1999 年底的第一次修订开始,

/* postfield data */
if('@' == *nextarg) {
  /* the data begins with a '@' letter, it means that a file name
     or - (stdin) follows */
  FILE *file;
  nextarg++; /* pass the @ */
Run Code Online (Sandbox Code Playgroud)

很难确定它是否特定于 cURL。语法是常见且自然的。与之关联的 cURL 功能是 cURL 的基本功能。如果有的话,类似于 cURL 的工具很可能会实现某种形式。


最初的问题问的是

$ echo foo | curl -d 'sprunge=<-' http://sprunge.us
Run Code Online (Sandbox Code Playgroud)

这是我的回答:

我不相信这是 cURL 的一个特性。

$ # Terminal A
$ curl --version
curl 7.31.0 (x86_64-unknown-linux-gnu) libcurl/7.31.0 OpenSSL/1.0.1e zlib/1.2.8 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP
$
$ echo foo | curl -d 'sprunge=<-' localhost:2222

$ # Terminal B
$ nc -l 2222
POST / HTTP/1.1
User-Agent: curl/7.31.0
Host: localhost:2222
Accept: */*
Content-Length: 7
Content-Type: application/x-www-form-urlencoded

sprunge=<-
Run Code Online (Sandbox Code Playgroud)

我在 cURL 文档中找不到任何提及此功能的内容。不过也有类似的功能。

如果您以字母@ 开头数据,则其余部分应该是从中读取数据的文件名,或者 - 如果您希望 curl 从 stdin 读取数据。文件的内容必须已经是 URL 编码的。也可以指定多个文件。因此,从名为“foobar”的文件中发布数据将使用 --data @foobar 完成。