Multipart Formpost Error

Please post support and help requests here.
Posts: 6
Joined: 17 Nov 2011, 21:32

Multipart Formpost Error

Postby mober » 17 Nov 2011, 21:41

Hi there,
I just started using the Poco Library and I've got some problems when I do a multipart formpost. here is the code I use. This is running on Snow Leopard with Poco Lib Version 1.4.2p1.

Code: Select all

        string file("/tmp/1.pdf");
        string aqs("/tmp/settings.aqs");

        URI uri(_baseUrl);
        string path = "/jobs.xml";

        HTTPClientSession session(uri.getHost(), uri.getPort());
        HTTPRequest request(HTTPRequest::HTTP_POST, path, HTTPMessage::HTTP_1_1);
        HTTPResponse response;

        // Multipart Form Post Data
        form.set("Filename", p.getFileName());
        form.set("access_token", _token);
        form.set("job_option", aqs);

        FilePartSource f1(file);
        form.addPart("Filedata", & f1);

        ostringstream ostr;
        form.write(ostr, "MIME_boundary_0123456789");

        request.setContentLength( ostr.str().length() );


        istream& rs = session.receiveResponse(response);

when I run into the last line an error gets thown at me that says.

malloc: *** error for object 0x10040bc00: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

I have no clue what I'm doing wrong here. Has anybody a hint for me what is going on and how I can get around that?

Posts: 1268
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Multipart Formpost Error

Postby guenter » 18 Nov 2011, 17:16

You need to create your FilePartSource object on the heap. HTMLForm will take ownership of it and delete it when it's done.

Posts: 6
Joined: 17 Nov 2011, 21:32

Re: Multipart Formpost Error

Postby mober » 18 Nov 2011, 19:58

Actually this did the trick! Thank you for pointing me in the right direction. Unfortunately there is not to much documentation about the FilePartSource in the docs. I should have adopted the new allocation as shown for the StringPartSource example in the HTMLFormTest.cpp suite.

I've got an other problem now. I'm talking to a rails app which uses rack middleware to store posted binary data in a temp directory -> /var/folders/w+/....
The binary data that is send to the rails server ends up in that folder as tempfile but has Zero KB size. I used a curl multipart formpost to compare both results. The request that hits the rails side looks exactly the same. But with the curl call the file has the right size in the temp folder. With the poco multipart post the filelength is 0. Do I have to set additional header information for this specific FilePart or for the entire request object?

The only thing that's slightly off is the CONTENT_LENGTH part which I figured is due to the different MIME_boundary in the request. Has somebody had similar problems like this?

Posts: 2
Joined: 11 Jun 2011, 13:35

Re: Multipart Formpost Error

Postby fusion44 » 12 Aug 2012, 01:36

Hi all,

sorry to dust off this thread. I have a similar problem like mober. Here's my code:

Code: Select all

 // prepare session
            Poco::URI uri(backend_url + "/api/update_character_avatar");
            Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());

            // prepare path
            std::string path(uri.getPathAndQuery());
            if (path.empty()) path = "/";

            Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, path, Poco::Net::HTTPRequest::HTTP_1_1);
            Poco::Net::HTMLForm form;
            form.add("token", sw_token);
            form.add("charname", sw_charname);

            Poco::Buffer<char> imgBuffer(mImgPtr->size());
            mImgPtr->read(imgBuffer.begin(), imgBuffer.end()-imgBuffer.begin());
            std::string s(imgBuffer.begin(), mImgPtr->size());

            std::ostringstream out;
            Poco::Base64Encoder b64enc(out);
            b64enc.write(imgBuffer.begin(), imgBuffer.end()-imgBuffer.begin());
            Poco::Net::StringPartSource *prtsrc = new Poco::Net::StringPartSource(out.str());
            form.addPart("imagedata", prtsrc);

            std::ostringstream ostr;

            std::ostream& send = session.sendRequest(req);

            // get response
            Poco::Net::HTTPResponse res;

When I std::cout << ostr.str() I see the information is correct.
This is the request thats being sent like seen by rawcap:

Code: Select all

/api/update_character_avatar HTTP/1.1
Connection: Close
Content-Length: 26586
Host: localhost:8081

Content-Disposition: form-data; name="charname"

Content-Disposition: form-data; name="token"

Content-Disposition: form-data; name="imagedata"
Content-Type: text/plain


As soon as I comment the line form.addPart("imagedata", prtsrc); so it doesn't add the partsrc the request works fine. But with the additional info I have an unlimited connection to my server (it never finishes) until I quit my program.
I use google's app engine. Here's the output from the appengine logs:

Code: Select all

ERROR    2012-08-11 23:25:17,480] Exception encountered handling request
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\", line 2842, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\", line 2708, in _Dispatch
    int(self.headers.get('content-length', 0)))
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\", line 353, in CopyStreamPart
    bytes =, COPY_BLOCK_SIZE))
  File "C:\Python27\lib\", line 380, in read
    data = self._sock.recv(left)
error: [Errno 10054] Eine vorhandene Verbindung wurde vom Remotehost geschlossen
INFO     2012-08-11 23:25:17,480] "POST /api/update_character_avatar HTTP/1.1" 500 -

Any idea what might be wrong?
Thanks for your help :)

Return to “Support”

Who is online

Users browsing this forum: No registered users and 3 guests