0

I have been using a single part http post form in my objective C to upload data to a server (PHP), and then a separate multipart post form to upload the files.

I have just decided to consolidate it into a single, multipart form for various reasons.

However, when using exactly the same objc on iPhone (meaning when I NSLog out what I am sending, it is percent encoded the same in both ways in doing it), and exactly the same php on the server I have realised that somehow the percent encoded strings lose their percent encoding along the way in the single part form, whereas with the multipart form the percent encoding is retained.

In the single part form the relevant section is like this:

params =[NSString stringWithFormat:@"string=%@", PERCENT_ENCODED_STRING];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

If I NSLog out params the encoding is there.

Whereas in the multipart form the relevant section is like this

    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];  //boundary of post data section
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: text/plain; name=%@\r\n", @"string"] dataUsingEncoding:NSUTF8StringEncoding]];  //dataname
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  //blank line
    [body appendData:[PERCENT_ENCODED_STRING dataUsingEncoding:NSUTF8StringEncoding]];  //actual data
    [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  //end of line
...
[request setHTTPMethod:@"POST"];
[request setHTTPBody:body];

The php is exactly the same in each case.

Has anyone come across this before?

I'm very aware that I could just be doing something stupid, but I've been looking at this all day without being able to figure out what is going on.

thanks in advance

narco
  • 830
  • 8
  • 21

1 Answers1

0

I have just found this

application/x-www-form-urlencoded or multipart/form-data?

and this

http://www.w3.org/TR/html401/interact/forms.html

It seems that perhaps what I have found is what is supposed to happen for the following reason: Single part forms are encoded (and presumably decoded by the php at the other end to cancel it out). Multipart forms are not encoded, because they deal with larger amounts of data and this could potentially make them much longer. So therefore I assume they are not decoded at the other end like I'm assuming single part forms are.

So, since I am handing them both an already encoded string, the php thinks it needs to decode the one from a single part form (since it doesn't know that it was actually me that encoded it, and I want it to stay that way), and the one from the multipart form just leaves it as is.

If I had started with a non encoded string, it should end up the same in both cases.

Perhaps..

Now assuming that is right, I just have to think what to do about it (the most elegant solution), as my app uses both single and multi part forms in different places, and I want the strings to remain encoded once they are handed to the php.

Community
  • 1
  • 1
narco
  • 830
  • 8
  • 21