Extracting attachement in PUT request

Skip to first unread message


Aug 17, 2014, 5:47:55 AM8/17/14
to moch...@googlegroups.com
Hi All: 

I'm looking right way to  extract attachment file in PUT request. 
Here is sample CURL request from client: 

curl -X PUT -d "id=101&api_key=xxx" -H "Content-Type:application/json" -T /tmp/test.wav http://localhost/api/apps

So in above request I want to receive test.wav audio file. 
Here is my little request parser to get other parameters: 

  RecvBody = Req:recv_body(),
  Data = case RecvBody of
    '>' -> erlang:list_to_binary("{}");
    Bin -> Bin
  case catch mochijson2:decode(Data) of 
{struct, Struct} -> Struct;
BadData -> [{}]

It seems decode() call is decoding all data together. 
Can you please point in right direction ? Any sample would be  highly appreciated.

Thanks a lot. 

Bob Ippolito

Aug 17, 2014, 2:57:46 PM8/17/14
to moch...@googlegroups.com
The only data in the recv_body is the wav file, the curl command doesn't send anything else, so decoding it as JSON is of course going to fail. Here's why the curl command line doesn't make any sense:

* The contents of -d is in application/x-www-form-urlencoded format, not application/json
* The -T option overrides -d entirely, so the id and api_key are never sent to the server, only the wav file

If you want to send files in addition to structured data, you have a few options:

* Use a multipart request (mochiweb_multipart can help you with this)
* Put the structured data on the query string, and send the file as the body
* Encode the file in the structured data (e.g. with base64)

The last option is not so great because you can't easily stream the file and it's typically a larger encoding. multipart is likely the best option but it depends on how easily you can have the client support it (curl does this with the -F option).



Aug 18, 2014, 3:21:04 AM8/18/14
to moch...@googlegroups.com
Hi Bob, 

Thanks for quick response. 
I'm able to use multipart request and get attachment using below sample : 

While I use curl command line tool to send request, it seems -d option can not be used with -F
so it seems there is no way to send other parameters ( api_key etc..)  without query string use. 
I'm able to send file like this : 
curl -v -X PUT -F file=@/tmp/test.wav http://localhost/api/apps

It would be very awkward to put api_key etc.. in query string. 
Is there anyway to send other params without query string without doing encoding? 


Bob Ippolito

Aug 18, 2014, 11:53:07 AM8/18/14
to moch...@googlegroups.com
Take a closer look at the curl documentation. You should be able to use -F more than once. 
You received this message because you are subscribed to the Google Groups "MochiWeb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mochiweb+u...@googlegroups.com.
To post to this group, send email to moch...@googlegroups.com.
Visit this group at http://groups.google.com/group/mochiweb.
For more options, visit https://groups.google.com/d/optout.


Aug 21, 2014, 7:02:26 PM8/21/14
to moch...@googlegroups.com
Yes..Bob.  That "-F" can be repeated, didn't know that..huh.  
Thanks a lot for help and I got everything working ! 
To unsubscribe from this group and stop receiving emails from it, send an email to mochiweb+unsubscribe@googlegroups.com.
Reply all
Reply to author
0 new messages