Httpie: 请求为 curl 等原始正文添加 `-d, --data` 选项

创建于 2016-10-31  ·  18评论  ·  资料来源: httpie/httpie

请求很简单,只需添加一个选项来传递原始数据,如curl所做的:

http :/api/user -d 'MyRawData...'

我知道,在大多数情况下,如果您发送 JSON 或表单数据,可以使用 _"request items"_ 来实现,例如:

http :/api/hey say=Hello to=me …

它会根据内容类型转换为正确的格式,太棒了! 如果您要发送的不是 JSON 或表单数据,您可以执行以下操作:

echo 'MyRawData...' | http :/api/hey

但这是不切实际的,HTTPie 的主要思想是_cURL-like tool for human_,而本例与此原理相去甚远,实际上curl比上例中的HTTPie 更实用。 添加多个命令并使用| <之类的丑陋字符对它们进行管道传输,仅仅因为缺少一个简单的选项听起来并不_human-friendly_。

-d选项添加到http有什么问题?

最有用的评论

你可以只做http POST example.org <<< "foo bar"http POST example.org < file.name

所有18条评论

将 -d 选项添加到 http 有什么问题?

这没有什么_特别_错。 我只是发现管道更清洁,并且非常喜欢只有一种方法可以做同样的事情。 管道的存在就是为了这个目的(即将_data_传递给程序),而且它易于理解、通用且明确。 每个像样的 CLI 工具都支持管道( curl除外),因此您只需要学习一次这个概念。

比较:

httpie

传递请求数据的通用方法是通过重定向标准输入(标准输入)。 此类数据被缓冲,然后不再作为请求主体进行进一步处理。

卷曲

-d, --data <data>
              (HTTP)  Sends  the  specified data in a POST request to the HTTP server, in the same way that a browser
              does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass
              the  data  to  the  server  using  the  content-type application/x-www-form-urlencoded.  Compare to -F,
              --form.

              -d, --data is the same as --data-ascii. --data-raw is almost the same  but  does  not  have  a  special
              interpretation of the @ character. To post data purely binary, you should instead use the --data-binary
              option.  To URL-encode the value of a form field you may use --data-urlencode.

              If any of these options is used more than once on the same command line, the data pieces specified will
              be merged together with a separating &-symbol. Thus, using '-d name=daniel -d skill=lousy' would gener-
              ate a post chunk that looks like 'name=daniel&skill=lousy'.

              If you start the data with the letter @, the rest should be a file name to read the data from, or -  if
              you  want  curl  to read the data from stdin. Multiple files can also be specified. Posting data from a
              file named 'foobar' would thus be done with --data @foobar. When --data is told to  read  from  a  file
              like  that,  carriage  returns  and newlines will be stripped out. If you don't want the @ character to
              have a special interpretation use --data-raw instead.

是的,我同意你在命令行工具中支持管道是一个很好的特性,而且我还制作了一个支持管道的命令行工具( Mongotail ),老实说我不知道curl不支持它。 但我认为支持这两种特性并不会增加复杂性,因为 Unix 生态系统中几乎所有已知的 CLI 工具都支持这两种方式。 例如。 cat , grep , find , tail ...

您提到的命令通常通过stdin接受文件名参数列表或原始输入数据。 但是,不接受实际数据作为参数。 通过参数接受原始数据是非常罕见的。

(澄清我在之前的评论中写的内容: curl确实支持标准输入,但需要明确指示使用例如--data-binary @-来阅读它。)

来到这里提交与此问题相关的错误,所以它可能不是错误,而是按设计工作。

我有一个 bash 脚本,我改用“httpie”而不是“curl”。 这些请求是到 http 服务器的空体 POST。 我通过将它管道到docker exec -i ${container} bash -x来运行这个脚本。

我很难弄清楚http POST命令虽然在从交互式 shell 运行时运行良好,但会导致脚本立即退出。

我想这与httpdocker exec $ 中读取标准输入有关。 我必须通过管道“ echo -n ”来避免这种情况似乎很奇怪。

#!/bin/bash
echo "STARTING..."
echo -n | http POST ...     # this replaces: curl -XPOST --data-binary '' ...
echo "Without the 'echo -n' above this statement would not be reached."
echo "DONE"

@jamshidPOST只是一个空的身体http POST httpbin.org/post 。请阅读在脚本中使用 HTTPie 的细节- 你想包括--ignore-stdin选项。这是不相关的问题,不过,如果需要,请打开一个新问题而不是在这里回复。)

我是否认为15.1 Request data from a filename涵盖了这个问题的原始请求? 我想这个问题可以关闭。

顺便说一句,我希望我在昨天之前就知道 HTTPie,因为我会避免 3 个小时左右的时间来弄清楚为什么我的 XML 文件中的换行符没有被保留。 (我以为这是我的应用程序,但它是 curl,需要使用它的--data-binary选项来保留数据。)感谢 HTTPie!

它没有, @DavidOliver@mrsarm请求能够从参数传递字符串,而不是文件的内容。

+1

您会接受带有此功能的 MR @jakubroztocil吗?

你可以只做http POST example.org <<< "foo bar"http POST example.org < file.name

http :/api/hey say=Hello to=me …

你可以只做http POST example.org <<< "foo bar"http POST example.org < file.name

似乎不适用于 powershell, 'raw body data' | http post :8080/api/events在 powershell 上为我工作,
但仍然想要-d, --data或类似的东西来传输原始身体数据

根据文档,您可以使用“Bash here string”:

http example.com/ <<<'{"name": "John"}'

在 UI 方面,该选项是有意义的。

我似乎找不到发送空 json 对象( {} )的方法,这是一个奇怪但有效的用例。

@minusf :我似乎找不到发送空 json 对象( {} )的方法,这是一个奇怪但有效的用例。

$ echo '{}' | http httpbin.org/post

重定向时有什么方法可以丢弃换行符?

$ echo 20 | http POST httpbin.org/post

提交的数据将是"data": "20\n"

@hahattan您可以指示echo不使用-n打印尾随换行符:

$ echo -n foo | http httpbin.org/post
此页面是否有帮助?
0 / 5 - 0 等级