Overview
Features
Download
Documentation
Community
Add-Ons & Services

multipart upload

A general discussion forum.

multipart upload

Postby CER » 02 Apr 2012, 19:10

Hello I am trying to upload some file (data.bin) to a server

Code: Select all
 57     HTTPClientSession s("example.com");
 58     HTTPRequest request(HTTPRequest::HTTP_POST, "/upload.php");
 59
 60     HTMLForm form(HTMLForm::ENCODING_MULTIPART);
 61     form.set("userid", "value1");
 62     form.set("password", "value2");
 63
 64     FilePartSource* pFPS = new FilePartSource("data.bin");
 65
 66     form.addPart("data.bin", pFPS);
 67     form.prepareSubmit(request);
 68
 69     request.setKeepAlive(true);
 70     request.setChunkedTransferEncoding(true);
 71     s.sendRequest(request);
 72
 73     request.write(std::cout);
 74     form.write(std::cout);
 75
 76     HTTPResponse response;
 77     std::istream& rs = s.receiveResponse(response);
 78
 79     Poco::StreamCopier::copyStream(rs, std::cout);



Above compiles but the server does not see any data. For instance, PHP's $_FILES is empty.

Thank for telling me what is wrong in the code above

Have a good day!
CER
 
Posts: 5
Joined: 20 Jan 2012, 00:04

Re: multipart upload

Postby CER » 05 Apr 2012, 16:44

Hi

I finally solved the issue. The problem was that the call to
s.sendRequest(request)
should be wrapped inside
form.write(s.sendRequest(request));
so that the form's body is really sent.

Below is a working solution
Code: Select all
85   using namespace Poco::Net;
 87   Poco::URI uri("http://example.com:80/upload.php");
 88   
 89   HTTPClientSession s(uri.getHost(), uri.getPort());
 90   HTTPRequest request(HTTPRequest::HTTP_POST, uri.getPathAndQuery(), HTTPMessage::HTTP_1_1);
 91   request.setHost(uri.getHost(), uri.getPort());
 92   request.set("User-Agent", "POCO 1.4.3p1");
 93   //request.setKeepAlive(true);
 94   //request.setChunkedTransferEncoding(true);
 95   
 96   HTMLForm form(HTMLForm::ENCODING_MULTIPART);
 97   form.set("userid", "value1");
 98   form.set("password", "value2");
 99   FilePartSource* pFPS = new FilePartSource("data.bin", "data.bin","application/octet-stream");
100   form.addPart("message", pFPS);
101   form.prepareSubmit(request);
102   
103   std::ofstream log("log",std::ios::app);
104   request.write(log);     //request.write(std::cout);
105   form.write(log);        //form.write(std::cout);
106   
107   //s.sendRequest(request);
108   form.write(s.sendRequest(request));
109   
110   HTTPResponse response;
111   std::istream& rs = s.receiveResponse(response);
112   
113   Poco::StreamCopier::copyStream(rs, std::cout);
114   
115   std::cout << response.getStatus() << ": " << response.getReason() << std::endl;
116   
117   return;

CER
 
Posts: 5
Joined: 20 Jan 2012, 00:04


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron