11

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?

2
  • 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? Commented 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' Commented May 14, 2023 at 11:39

2 Answers 2

13

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

Sign up to request clarification or add additional context in comments.

5 Comments

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, 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
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.
@sradforth did you find out how to do it with NodeJS?
@Antirreni91 were either of you able to figure out how to do it with NodeJS? Very curious about whether its possible.
5

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

1 Comment

Somewhat off topic, but any idea how to get the curl that PostMan generates to use --data-binary? Seems like even if you select binary, it still forces it to use --data and not --data-binary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.