×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    119

    HTTP Post request structure

    Hello,

    I am trying to send a request via HTTP Post. The session and transaction are created and submitted correctly, as I do execute a php script waiting on a www server. However, the structure of the request body seems to be incorrect, as I can't read POST variables in my script. When I send the request via web browser everything is correct, so the problem is in my code.

    I've read and used the HTTP Client Example, but they don't construct a Post request there, they leave it to the user submitting the data using GUI.

    Here's how I construct the request body (it's for testing purposes only):

    delete iPostData;
    TBuf8<1024> tmp;
    _LIT8(KPost1, "--------------1345234fsf342\n");
    _LIT8(KPost2, "Content-Disposition: form-data; name=\"latitude\"\n\n");
    _LIT8(KPost3, "22.5");
    _LIT8(KPost4, "--------------1345234fsf342--");
    tmp.Copy(KPost1);
    tmp.Append(KPost2);
    tmp.Append(KPost3);
    tmp.Append(KPost4);

    iPostData = tmp.AllocL();

    _LIT8(KUrl, "http://myscript.com");
    TUriParser8 uri;
    uri.Parse(KUrl);

    RStringF method = iSession.StringPool().StringF(HTTP::EPOST,
    RHTTPSession::GetTable());

    iTransaction = iSession.OpenTransactionL(uri, *this, method);

    _LIT8(KUserAgent, "MyClient");
    _LIT8(KAccept, "*/*");
    _LIT8(KMimeType, "multipart/form-data; boundary =--------------1345234fsf342");

    RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
    SetHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
    SetHeaderL(hdr, HTTP::EAccept, KAccept);
    SetHeaderL(hdr, HTTP::EContentType, KMimeType);

    MHTTPDataSupplier* dataSupplier = this;
    iTransaction.Request().SetBody(*dataSupplier);

    // Submit the transaction. After this the framework will give transaction
    // events via MHFRunL and MHFRunError.
    iTransaction.SubmitL();

    /////

    GetNextDataPart(TPtrC8 &aDataPart)
    {

    aDataPart.Set(iPostData->Des());

    return ETrue;

    }


    Can you see any errors?

    Maciej

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,694

    Re: HTTP Post request structure

    Note that multipart uses the boundary-string as --boundary-string for marking continuation and --boundary-string-- for marking the last part. You have 14 hyphens everywhere.

  3. #3
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    119

    Re: HTTP Post request structure

    Indeed, that was the case, and some missing new line signs as well. Thank you.

    Now I am able to transfer all my data successfully. However, if I send my image encoded using the base64 algorithm, the webserver doesn't recognize it as an image... It is saved on the server, but it can't be read as JPG.

    Here's how I put it in the request body:

    _LIT8(KNewLine, "\n");
    _LIT8(KPost1, "--1345234fsf342\n");
    _LIT8(KPost2, "Content-Disposition: form-data; name=\"photo\"; filename=\"test.jpg\"\nContent-Transfer-Encoding: base64\n\n");
    _LIT8(KPost3, "--1345234fsf342--\n");

    tmp.Append(KPost1);
    tmp.Append(KPost2);
    tmp.Append(aPhoto);
    tmp.Append(KNewLine);
    tmp.Append(KPost3);

    where aPhoto is a Ptr8 containing a base64 encoded photo.

    If I send the base64 string directly, as text/plain, save it in a file on the webserver and use an external base64 decoder, the image is decoded correctly - so it is not a matter of encoding error.

    I suppose that I am putting the image data in the request body in an incorrect way and because of that the webserver can't read it in a proper way, it just decodes it to a file of an unrecognizable format.

    Are you able to spot any errors in my code?

    Maciej

  4. #4
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    119

    Re: HTTP Post request structure

    I saved the request body to a file to see if it's correct, here it goes:

    --1345234fsf342
    Content-Disposition: form-data; name="latitude"

    22.5
    --1345234fsf342
    Content-Disposition: attachment; name="photo"; filename="ph.jpg"
    Content-Type: image/jpeg
    Content-Transfer-Encoding: base64

    /9j/4RATRXhpZgAASUkqAAgAAAAIAA8BAgAGAAAAbgAAABABAgAEAAAARTUwABIBAwABAAAAAQA
    AABoBBQABAAAAdAAAABsBBQABAAAAfAAAACgBAwABAAAAAgAAABMCAwABAAAAA[...]QLU
    N4o3igBpamlqBWP//Z
    --1345234fsf342
    Content-Disposition: form-data; name="longitude"

    123.54
    --1345234fsf342--

    Is there anything wrong?

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,694

    Re: HTTP Post request structure

    Sorry I have absolutely no idea how HTTP POST works on the server side, let us hope that others know and they are going to share...

  6. #6
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    119

    Re: HTTP Post request structure

    There is a very useful tool named Live HTTP Headers - http://livehttpheaders.mozdev.org/

    It lets you track the requests made by the browser. That's how I finally managed to make my post requests work - I just made a form which executed my script and viewed the request.

    It didn't differ a lot, actually, but I saw that the browser doesn't use the base64 encoding, so I gave up on it and decided to send binary data. It works fine now, with the same structure as posted above, the only difference is:

    ----1345234fsf342
    Content-Disposition: form-data; name="photo"; filename="ph.jpg"
    Content-Type: image/jpeg

    Without specifying encription type.

  7. #7
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Germany
    Posts
    115

    Re: HTTP Post request structure

    Hi Alav,

    I also need to upload a file from a cellphone to a server over http.
    My file is e.g. C:\\Data\\myFile.xml
    and I have also an uploadscript.php on the server
    e.g. www. mydomain.com/myapp/uploadscript.php

    Could you please give me a brief summary of how to make this happened?

    It would be highly appreciated!
    Cheers, Luuca

  8. #8
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    131

    Unhappy Re: HTTP Post request structure

    Quote Originally Posted by alav View Post
    Indeed, that was the case, and some missing new line signs as well. Thank you.

    Now I am able to transfer all my data successfully. However, if I send my image encoded using the base64 algorithm, the webserver doesn't recognize it as an image... It is saved on the server, but it can't be read as JPG.

    Here's how I put it in the request body:

    _LIT8(KNewLine, "\n");
    _LIT8(KPost1, "--1345234fsf342\n");
    _LIT8(KPost2, "Content-Disposition: form-data; name=\"photo\"; filename=\"test.jpg\"\nContent-Transfer-Encoding: base64\n\n");
    _LIT8(KPost3, "--1345234fsf342--\n");

    tmp.Append(KPost1);
    tmp.Append(KPost2);
    tmp.Append(aPhoto);
    tmp.Append(KNewLine);
    tmp.Append(KPost3);

    where aPhoto is a Ptr8 containing a base64 encoded photo.

    If I send the base64 string directly, as text/plain, save it in a file on the webserver and use an external base64 decoder, the image is decoded correctly - so it is not a matter of encoding error.

    I suppose that I am putting the image data in the request body in an incorrect way and because of that the webserver can't read it in a proper way, it just decodes it to a file of an unrecognizable format.

    Are you able to spot any errors in my code?

    Maciej
    hi can u share with me on how u managed to get the right string representation for "aPhoto"?

    thanks =)

    Code:
    			RFileReadStream inputStream(iFile);
    			MStreamBuf* streamBuf;
    
    			aSize=aSize/500;
    			HBufC8* mediaFile;
    			mediaFile=HBufC8::NewLC(aSize);
    			TPtr8 bufferPtr(mediaFile->Des());
    			//iFile.Read(bufferPtr);
    			streamBuf=inputStream.Source();
    			TRequestStatus readStatus;
    			streamBuf->Read(bufferPtr,readStatus);
    			User::WaitForRequest(readStatus);
    			streamBuf->Close();
    			inputStream.Release();
    i've tried this way but no luck....
    i get some sort of string but when i use that to upload to my server the byte size seems too small...

    pls help

Similar Threads

  1. http request error -33
    By vimal r jain in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2007-03-06, 07:59
  2. Problem with HTTP POST
    By thathoo in forum Mobile Java General
    Replies: 2
    Last Post: 2006-12-18, 18:23
  3. Problems with HTTP POST
    By Summerman in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2006-11-27, 09:25
  4. 7650 J2ME/MIDP HTTP Post
    By cwalkinshaw in forum Mobile Java General
    Replies: 2
    Last Post: 2002-10-23, 10:12
  5. Replies: 0
    Last Post: 2002-05-09, 12:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×