Overview
Features
Download
Documentation
Community
Add-Ons & Services

How to POST a image using POCO::Net::HTTP

Please post support and help requests here.

How to POST a image using POCO::Net::HTTP

Postby leezhm » 31 Aug 2012, 16:51

Here is my code
Code: Select all
      std::string strURI = "http://porsche.herokuapp.com/api/addphotoboothimage";

      Poco::URI uri(strURI);
      std::string path(uri.getPath());

      Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
      Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPMessage::HTTP_1_1);
      
      session.setKeepAlive(true);

      //std::string body = "{\"uuid\":\"3e8abe18-1881-46a7-8c55-6ba1e8841937\",\"photobooth\":\"xxxx\"}";
      //std::string body = "uuid=3e8abe18-1881-46a7-8c55-6ba1e8841937";
      //req.setContentLength(body.length());
      
      req.setContentType(Poco::Net::HTMLForm::ENCODING_MULTIPART);

      //std::cout<<"Host: "<<uri.getHost()<<" path: "<<uri.getPath()<<"\njson = "<<body<<std::endl;

      Poco::Net::HTMLForm form(Poco::Net::HTMLForm::ENCODING_MULTIPART);
      //form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);d18ff4f7-4eb9-4e95-bfc5-e141ec1d9d7b
      //form.add("uuid", "3e8abe18-1881-46a7-8c55-6ba1e8841937");
      form.add("uuid", "d18ff4f7-4eb9-4e95-bfc5-e141ec1d9d7b");

      Poco::Net::FilePartSource * png = new Poco::Net::FilePartSource("E:\\of_release_v0071_vs2010\\examples\\empty\\emptyExample\\bin\\data\\image_to_upload.jpg", "application/octet-stream");
      png->headers().set("uuid", "d18ff4f7-4eb9-4e95-bfc5-e141ec1d9d7b");
      form.addPart("photobooth", png);

      form.prepareSubmit(req);
      
      std::ostream & send = session.sendRequest(req);
      req.write(std::cout);
      form.write(send);

      //send << body <<"\n";

      Poco::Net::HTTPResponse response;

      //response.setContentType("application/octet-stream");
      //response.setTransferEncoding("charset=utf-8");

      std::istream & res = session.receiveResponse(response);

      std::ostringstream stream;
      Poco::StreamCopier::copyStream(res, stream);

      std::cout<<stream.str()<<std::endl;


Actually, It does not upload successful.Does any one can help me?
leezhm
 
Posts: 1
Joined: 31 Aug 2012, 16:28

Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests