2

How do you POST a binary variable in curl bash?

#!/usr/bin/env bash
IMAGE=$(curl "http://www.google.com/images/srpr/logo3w.png")
curl --data-binary "$IMAGE" --request "POST" "http://www.somesite.com"

Curl seems to do corrupt the image when uploading.

Curl has the option to write response to disk and then read from it, but it'd be more efficient to do it solely in memory.

2
  • Can you pipe the output of the first curl command into the second? This at least eliminates the variable ... Commented Jan 6, 2013 at 20:55
  • @ernestopheles Could you show me how? Commented Jan 6, 2013 at 20:57

2 Answers 2

2

Try to eliminate the variable ... as follows:

curl "http://www.google.com/images/srpr/logo3w.png" | curl --data-binary - --request "POST" "http://www.somesite.com"

From the curl man page:

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin.

EDIT: From the man page, too:

--raw When used, it disables all internal HTTP decoding of content or transfer encodings and instead makes them passed on unaltered, raw. (Added in 7.16.2)

What happens, if applied on either or both sides?

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

3 Comments

curl "google.com/images/srpr/logo3w.png" | curl --data-binary @- --request "POST" "somesite.com" still mangles the binary data.
@BeniBela no, its part of a multipart post. Everything else works, but the binary data is getting mangled.
raw still doesn't work. Thanks for the help guys, but I'll write it own in pycurl.
1

I had a related problem, where I wanted to dynamically curl a file from a given folder.

curl --data-binary directory/$file --request "POST" "http://www.somesite.com" did not work - uploaded the string "directory/myFile.jar" instead of the actual file.

Adding the @ symbol curl --data-binary @directory/$file --request "POST" "http://www.somesite.com" fixed it.

Comments

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.