添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Using this [ https://github.com/prometheus/pushgateway][1] we are trying to push one metric to prometheus. It seems to require the data in a very specific format.

It works fine when doing their example curl of

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

Yet doing a curl with -d option fails as missing end of line/file

curl -d 'some_metric 3.15\n' http://pushgateway.example.org:9091/metrics/job/some_job

I'm trying to understand the difference in behaviour since I believe both are doing POST commands and I need to replicate this --data-binary option in node.js via "request.post" method but I seem to only be able to replicate the curl -d option which doesn't work.

Any suggestions on hints on what the difference is between -d and --data-binary and to do the equivalent to --data-binary from within node.js?

why are you combining questions here. Are you trying to understand the behaviour of curl command with its different flags? or are you trying to send a NodeJS POST request to push metrics to push Gateway? – Andre Leon Rangel Mar 6, 2019 at 4:55 Your second command doesn't work because '\n' in Bash isn't a newline, it's the letter "n". You want to use ANSI C quoted strings: curl -d $'some_metric 3.15\n' – Boris Verkhovskiy May 14 at 11:39

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

Using @- will make curl read the filename from stdin.

So, basically in your first variant you send a binary file named "some_metric 3.14". In the second one, you're sending an ascii string "some_metric 3.15\n".

If you want curl to strip new lines before sending, use --data-ascii or -d option:

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

Many thanks for your detailed response. Any idea how I can get node.js to POST data that matches the --data-binary curl command? It seems it should just be a case of adding a \n to the data it is POSTing? The weird thing seems that the \n approach didn't seem to work hence why I'm curious what else it does differently. – sradforth Nov 15, 2018 at 12:24 @sradforth, I'm not quite familiar with NodeJS but you can take a look here: stackoverflow.com/questions/38030484/… Also, it'd be helpful if you send a file you're trying to send and a piece of code that does the file upload – StasKolodyuk Nov 15, 2018 at 12:41 About posting metric into push gateway - do not forget to add "\n" at the end of line. Without this you will get 400 error code. – Cherry Mar 20, 2019 at 11:22 @Antirreni91 were either of you able to figure out how to do it with NodeJS? Very curious about whether its possible. – Kevin Andres Apr 30, 2021 at 6:16

-d (or --data) and --data-binary are exactly the same if you're just passing a normal value like this:

curl --data somekey=somevalue example.com

But if you're telling curl to read the value from a file by passing --data @filename (or @- to read from stdin) like this:

curl --data @./some/path/to/a/file.txt example.com

then the difference is that --data removes all \n and \r characters in the file, whereas --data-binary just sends the file as-is.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.