views:

268

answers:

2

I create NSMutableUrlRequest for sending data to server, add all necessary fields to it and then add the string for sending like this:

[theRequest setHTTPBody:[postString dataUsingEncoding: NSUTF8StringEncoding]];

postString is a usual NSString.

The problem is, when I receive this request at the server, all the plus (+) signs disappear from the http body. So if I had "abcde+fghj" on iPhone, I get "abcde fghj" on the server".

Can this be some encoding problem from using dataUsingEncoding: NSUTF8StringEncoding? Or some NSMutableUrlRequest stripping feature? What can I do to make it stop stripping plus signs? I need to receive UTF8 strings at the server side.

+1  A: 

The plus (+) sign is a standard shortcut for a space, in a URL's query string portion. If you want a literal +, encode it as %2b.

Frank Shearar
I don't dispute your answer, but shouldn't this only happen if the string is passed through the method `stringByAddingPercentEscapesUsingEncoding:` and not just by encoding it using UTF8?
Jasarien
thanks, now I figured it out. It seems I needed to run my string through the stringByAddingPercentEscapesUsingEncoding: first, then I needed to run it through replaceOccurrencesOfString:@"+" withString:@"%2B" and several more of those replaces for different characters, because stringByAddingPercentEscapesUsingEncoding: doesn't escape them all
Mike
A: 

Hi,

May be the server doesn't know which is the encoding of the POST body. Have you tried to add the charset=UTF-8 into the header of your request like that:

[theRequest setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
Yannick L.
thanks, I had application/x-www-form-urlencoded, but didn't have charset=UTF-8, I added that too now
Mike
update: just adding "application/x-www-form-urlencoded; charset=UTF-8" doesn't help, I still need to replace characters to % values, the whole POST body needs to be encoded heavily...
Mike