Using HTTPClient to post 2 key/value pairs + file data at once

20 Feb 2011

I'm trying to figure out how to use HTTPClient to upload a picture using the TwitPic API. The API requires that you send some headers (for authorization), then two key/value pairs, one of which includes the image data. The curl command shown below is an example:

   curl -v 
      -H 'X-Auth-Service-Provider:' 
       -H 'X-Verify-Credentials-Authorization: OAuth realm="", oauth_consumer_key="GDdmIQH6jhtmLUypg82g", oauth_signature_method="HMAC-SHA1", oauth_token="819797-Jxq8aYUDRmykzVKrgoLhXSq67TEa5ruc4GJC2rWimw", oauth_timestamp="1272325550", oauth_nonce="oElnnMTQIZvqvlfXM56aBLAf5noGD0AQR3Fmi7Q6Y", oauth_version="1.0", oauth_signature="U1obTfE7Rs9J1kafTGwufLJdspo%3D"' 
      -F "key=098f6bcd4621d373cade4e832627b4f6" 
       -F "media=@/path/to/image" 

I think I can use

setRequestHeader("X-Verify-Credentials-Authorization: OAuth realm=\"http:\\ [...and so forth...]")

to deal with the first two lines. But is there any way to send two key/value pairs, one of which reads part of its data from a file, all in the same POST using HTTPClient? Maybe the secret is to use HTTPStream?

14 May 2011

If what you suggested didn't work, here's an alternative, (which I've not used from mbed, but have from other services). Do 2 http requests, first of which does the authentication/login, the second does the upload. To make the remote server know that you are the same user, you'll need to save the cookie for the session id. The first response will have a header to "set cookie', grab the cookie name and value, and then set it as a request header in the 2nd request. Use a tool (like Fiddler (http proxy)) to catch the traffic between your browser and the remote server & you can examine the headers. Tony.