5

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.

Mike
  • 1,712
  • 3
  • 17
  • 17

3 Answers3

4

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
  • 17,012
  • 8
  • 67
  • 94
  • 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 Mar 22 '10 at 10:51
  • 2
    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 Mar 22 '10 at 11:10
0

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 Loriot
  • 7,107
  • 2
  • 33
  • 56
  • thanks, I had application/x-www-form-urlencoded, but didn't have charset=UTF-8, I added that too now – Mike Mar 22 '10 at 11:12
  • 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 Mar 22 '10 at 11:25
0

For e.g. you need to post data to the backend with a "Content-Type" -> "application/x-www-form-urlencoded"

application/x-www-form-urlencoded: the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value. Non-alphanumeric characters in both keys and values are percent encoded: this is the reason why this type is not suitable to use with binary data (use multipart/form-data instead)

Reference

You can percent-encode your data by applying function addingPercentEncoding to your string in Swift:

guard let jsonString = String(data: jsonData, encoding: .utf8)?.addingPercentEncoding(withAllowedCharacters: .alphanumerics) else {
   failureCompletion()
   return
}

var request = URLRequest(url: url)
...
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpBody = "jsonString".data(using: .utf8)
...
Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39