Passing binary data to curl without using a @file

Is it possible to use curl and post binary data without passing in a file name? For example, you can post a form using binary via –data-binary:

curl -X POST --data-binary @myfile.bin http://foo.com

However, this requires that a file exists. I was hoping to be able to log HTTP calls (such as to rest services) as the text of the curl command to reproduce the request. (this greatly assists debugging these services, for example)

However, logging curl commands that reference a file would not be useful, so I was hoping I could actually log the raw binary data, presumably base64 encoded, and yet still allow you to copy and paste the logged curl command and execute it.

So, is it possible to use curl and post binary data without referencing a file? If so, how would that work? What would an example look like?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can pass data into curl via STDIN like so:

echo -e '...data...n' | curl -X POST --data-binary @- http://foo.com

The @- tells curl to pull in from STDIN.

To pipe binary data to curl (for example):

echo -e 'x03xF1' | curl -X POST --data-binary @- http://foo.com

Method 2

Adding to this answer, the echo command appends a newline to its output by default. This adds a n to the end of your binary data, therefore curl will receive that character too as input.

To avoid that, you can use the printf command or the -n switch like so:

printf 'x03xF1' | curl -X POST --data-binary @- http://foo.com

or

echo -en 'x03xF1' | curl -X POST --data-binary @- http://foo.com

This way no newline will be appended to the curl input and the bytes being fed to curl will be exactly those you pass to echo.

Method 3

Not sure why, but the exact command line that slm suggested didn’t work for me. With a slight modification, the following worked:

echo -e '...data...n' | curl -s -T - sftp://<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a2f293f281a6b6a746b6a746b6a746b6a">[email protected]</a>/~/test.txt


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x