Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl -d vs --data-binary

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?

like image 219
sradforth Avatar asked Jan 28 '23 03:01

sradforth


1 Answers

From curl man page:

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

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

like image 179
StasKolodyuk Avatar answered Jan 31 '23 21:01

StasKolodyuk