1

Bash n00b here.. I'm posting a file b64 encoded like this using curl:

$ cat file.txt | openssl base64 | curl --data @- myhost.com/api

Works good. I split the key/value on the server side, the entire message goes into the key, but that's ok I parse it out and convert to ascii etc.. on the server.

How can I append other key/values to the post? Something like..

$ cat file.txt | openssl base64 | PREPEND "key=value1&key2&value2&btext=" | curl --data @- myhost.com/api
C B
  • 12,482
  • 5
  • 36
  • 48
  • possible duplicate of [HTTP POST and GET using cURL in linux](http://stackoverflow.com/questions/14978411/http-post-and-get-using-curl-in-linux) – AndRSoid Jan 04 '14 at 15:38
  • Guys/gals - before you slap a duplicate on this.. This is a Bash question, not a Curl question and is not a basic question about how to POST in Curl, which is the article that's been cited, and is not answered in the other article. – C B Jan 04 '14 at 15:47

2 Answers2

7

You can create all of the input to curl in a subshell, like so:

(echo -n "key=value1&key2=value2&btext="; openssl base64 < file.txt) | curl --data @- myhost.com/api

This will execute echo and openssl after each other and pass concatenated output to curl.

Sean
  • 29,130
  • 4
  • 80
  • 105
  • You may use `echo -n` to prevent the newline between `btext=` and the base64 data. – buc Jan 04 '14 at 15:43
2

Instead of @-, you can use -F to post both your prepended keys and the base64 text.

curl -F key=value1 -F key2=value2 -F btext=$(openssl base64 < file.txt | tr -d "\n") myhost.com/api

In my testing, base64 won't be sent correctly unless you remove linebreaks, and neither -F nor @- will work for particularly large files (>~50kb?).

ride
  • 129
  • 2
  • 2
  • 2
    Can you give a description of that? Your answer is currently incomplete. – bjb568 Mar 25 '14 at 01:12
  • @- refers to the usage in Sean's answer (piping btext into | curl --data @- instead of writing it using a $(subshell)). The >~50kb limit is what I assume to be the maximum size of a bash command vel alii. – ride Apr 15 '14 at 09:46