Curl -d 与 --data-binary

Curl -d vs --data-binary

使用这个 [https://github.com/prometheus/pushgateway][1] 我们试图将一个指标推送到普罗米修斯。似乎需要非常特定格式的数据。

在执行

的示例卷曲时效果很好
echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job

然而,由于 line/file

的结尾缺失,使用 -d 选项执行 curl 失败
curl -d 'some_metric 3.15\n' http://pushgateway.example.org:9091/metrics/job/some_job

我试图理解行为上的差异,因为我相信两者都在执行 POST 命令,我需要通过 [=23= 在 node.js 中复制此 --data-binary 选项] 方法,但我似乎只能复制不起作用的 curl -d 选项。

关于提示 -d 和 --data-binary 之间的区别以及在 node.js 中做相当于 --data-binary 的任何建议?

来自 curl 手册页:

--data-ascii

(HTTP) This is just an alias for -d, --data.

--data-binary

(HTTP) This posts data exactly as specified with no extra processing whatsoever.

If you start the data with the letter @, the rest should be a filename. Data is posted > in a similar manner as -d, --data does, except that newlines and carriage returns are > > preserved and conversions are never done.

Like -d, --data the default content-type sent to the server is application/x-www-form-> > urlencoded. If you want the data to be treated as arbitrary binary data by the server > then set the content-type to octet-stream: -H "Content-Type: application/octet-stream".

If this option is used several times, the ones following the first will append data as > described in -d, --data.

使用 @- 将使 curl 从 stdin.

读取文件名

因此,基本上在您的第一个变体中,您发送了一个名为“some_metric 3.14”的二进制文件。 在第二个中,您要发送一个 ascii 字符串“some_metric 3.15\n”。

如果你希望 curl 在发送前去除新行,使用 --data-ascii-d 选项:

echo "some_metric 3.14" | curl -d @- http://pushgateway.example.org:9091/metrics/job/some_job