|
|
Home » Developing U++ » U++ Developers corner » MultipartForm Class for use w/HttpClient
MultipartForm Class for use w/HttpClient [message #28074] |
Sat, 14 August 2010 17:18 |
jeremy_c
Messages: 175 Registered: August 2007 Location: Ohio, USA
|
Experienced Member |
|
|
I created a class named MultipartForm that can be used w/Http(s)Client. It allows one to do file uploads. I know this will be of use to others as the question has come up on the forum a few times. What I don't know is the best way to share it or get feedback on it. Here's a sample use:
MultipartForm form;
form.Set("name", "John Doe");
form.Set("age", "18");
form.SetFile("bio", "mybio.txt", LoadFile("mybio.txt"));
form.SetFile("avatar", "me.jpg", Base64Encode(LoadFile("me.jpg")), "base64");
HttpClient http("http://localhost/upload.cgi");
http.ContentType(form.GetContentType());
http.Post(form.ToString());
That's basically it. In the end it creates a properly formatted multipart/form-data submission.
Jeremy
|
|
|
|
Re: MultipartForm Class for use w/HttpClient [message #28082 is a reply to message #28074] |
Sat, 14 August 2010 21:46 |
jeremy_c
Messages: 175 Registered: August 2007 Location: Ohio, USA
|
Experienced Member |
|
|
Simply put the existing HttpClient does not provide the facilities for file uploads via HTTP. It does not support Multipart Form data. It supports the POST format just fine which is suitable for most form data, but when a file upload is required (or even present) then the HttpClient falls short. That's where this class comes into play. It builds the post data for HttpClient.
The example I gave above would produce post data that looks like:
form.GetContentType() returns:
Content-Type:multipart/form-data; boundary=XgBtCfDkTmZlYqMtPcLgFgY
form.ToString() returns:
--XgBtCfDkTmZlYqMtPcLgFgY
Content-Disposition: form-data; name="name"
John Doe
--XgBtCfDkTmZlYqMtPcLgFgY
Content-Disposition: form-data; name="age"
18
--XgBtCfDkTmZlYqMtPcLgFgY
Content-Disposition: form-data; name="bio"; filename="mybio.txt"
Content-Transfer-Encoding: text/plain
Hello World!
I am a person who does not exist but is always used in examples.
--XgBtCfDkTmZlYqMtPcLgFgY
Content-Disposition: form-data; name="avatar"; filename="me.jpg"
Content-Transfer-Encoding: base64
...bunch of data snipped for the posts sake...
--XgBtCfDkTmZlYqMtPcLgFgY--
[Updated on: Sat, 14 August 2010 21:48] Report message to a moderator
|
|
|
|
Re: MultipartForm Class for use w/HttpClient [message #28088 is a reply to message #28074] |
Sun, 15 August 2010 03:52 |
jeremy_c
Messages: 175 Registered: August 2007 Location: Ohio, USA
|
Experienced Member |
|
|
I was originally thinking of doing that, however the existing HttpClient doesn't provide a way to generate the url encoded post data either. Right now that's all done externally. For example, say I want to submit a form with name, email and password. Currently you would:
String postData = "name=" + UrlEncode(name) + "&email=" + UrlEncode(email) + "&password=" + UrlEncode(password);
HttpClient http("http://localhost/post.cgi");
http.Post(postData);
http.Execute();
So, I kept w/the same type of synario:
MultiformPost form;
form.Set("name", name);
form.Set("email", email);
form.Set("password", password);
HttpClient http("http://localhost/post.cgi");
http.Post(form.ToString());
http.ContentType(form.GetContentType());
http.Execute();
It's just that a Multipart Form is a more complex post structure than a url encoded post structure. Thus a helper class is just about essential. I did do multipart form uploads before w/o my helper class, but the helper class is a big help
Jeremy
[Updated on: Sun, 15 August 2010 03:55] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Sat Sep 21 06:17:38 CEST 2024
Total time taken to generate the page: 0.03348 seconds
|
|
|