Using http_request for a POST request that uploads a file

Apr 8, 2014 at 8:51 PM
It's my understanding that in order to upload a file with a POST request, I need to use multipart/form_data encoding.
Is there a way to do this with a http_request?
Or is there another way to properly format a POST request for this purpose?
Apr 11, 2014 at 1:55 AM
Hi,

You can upload a file without multipart/form_data encoding.
The http_request class has a set_body() overload that takes a Casablanca input stream. This input stream can be a file stream, pointing to the file to be uploaded.
By default, the content-type is set to "application/octet-stream", however you could change that to any other content-type.

I would suggest glancing through the test case: set_body_stream_1 and set_body_stream_2 at https://casablanca.codeplex.com/SourceControl/latest#Release/tests/Functional/http/client/request_stream_tests.cpp
These tests create an http_client, create an http_request for the POST request, set casablanca::streams::istream (to a file) as the body of the http_request.
On calling http_client::request(), the file is uploaded to the server.

Let me know if you have further questions.

Thanks
Kavya
Jun 9, 2014 at 9:15 PM
Hi,

Sorry for the (very) slow response, I was pulled off of this for a while.

Thanks for your help. I looked at the test cases you linked to, but I don't see where you would give the server a file name for saving what you're uploading. Am I missing something?
Please excuse my newbie-ness (is that a word?), but I don't have a very deep understanding of http requests and how they work.
Jun 12, 2014 at 1:39 AM
The client can only send data to the server. An http_client cannot tell the server where to store the data.

It is upto the server on how it wants to interpret and store the data. If you have control of the HTTP server, on receiving a request from the client, the server can save it to a file.

Thanks
Kavya.