curl with base64 encoded data

I just spent the last hour trying to figure out how to send base64 encoded data that lives in a file using curl. Everyone says to just use --data-binary but that assumes you know what the binary format should actually be! Every combination I tried of --data-ascii and --crlf kept failing with server-side errors.

The trick for me was making sure that the file itself was manually URL encoded. So all newlines were replaced with %0a and all plus signs were replaced with %2b. Then just put everything on one line. So my file (which was a SAML payload for testing) looked like:

                                                                 +                     \n

This can just be sent using the normal --data parameter as a file:

curl -XPOST --data @saml.txt

2 thoughts on “curl with base64 encoded data

  1. Thanks for sharing this way. I was struggeling while POSTing data with special chars (#;?$&) using a script (cygwin).

    I thougt about “sending the data as base64-encoded string” and found this site. I was thinking to do something like ” –data ‘[base64-encoded-content]’ ” but the webserver did not undetstand my data (was not decoding the base64 to plain text).

    So I did “your trick” to save the data in a separate file and POSTed it with ” –data @plaintextfile.txt ” … it worked.

    Thanks again for sharin your way!


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.