0

I send post request with data to my server:

var syncData = "data=Hello&World"
let urlRequest = NSMutableURLRequest(URL: NSURL(string: url)!, cachePolicy:  NSURLRequestCachePolicy.ReloadIgnoringCacheData, timeoutInterval: NSTimeInterval(60))

urlRequest.HTTPMethod = "POST"
urlRequest.HTTPBody = syncData.dataUsingEncoding(NSUTF8StringEncoding)

But problem that server can't decode &(ampersand) character. On server i get only word "Hello". And it is not the server issue. because i tried to sent this string with java and python and curl.
All works good.

I tried this: NSXMLParser problem with "&"(Ampersand) character

but it doesn't help... Any ideas ?

Community
  • 1
  • 1
Arti
  • 7,356
  • 12
  • 57
  • 122
  • Can you clarify what exactly happens? – Pekka Dec 29 '15 at 21:06
  • server decode only part of string. For example if string was Hello&World, in post data server decode only hello... – Arti Dec 29 '15 at 21:08
  • This is what happens: http://stackoverflow.com/questions/34515331/werkzeug-raises-brokenfilesystemwarning – Arti Dec 29 '15 at 21:09
  • 1
    Your data is not properly encoded, `&` has a special meaning and needs to be escaped so your string should be `data=Hello%26World` . – Musa Dec 30 '15 at 13:31

2 Answers2

1

As Musa said. Solution will be:

let customAllowedSet =  NSCharacterSet(charactersInString:"=\"#%/<>?@\\^`{|}&").invertedSet
    syncData = syncData.stringByAddingPercentEncodingWithAllowedCharacters(customAllowedSet)!

    urlRequest.HTTPBody = syncData.dataUsingEncoding(NSUTF8StringEncoding)
Arti
  • 7,356
  • 12
  • 57
  • 122
0

try

var sync = syncData.stringByReplacingOccurrencesOfString("&", withString: " ")
urlRequest.HTTPBody = sync.dataUsingEncoding(NSUTF8StringEncoding)

place the sync variable under the syncData variable and replace your urlRequest with the one above using the new sync variable

  • yes, replace is the solution, but it is very bad solution :( I need some unique character to replace. Because i haven't only urls, i have large texts too, that can contains ampersands and spaces – Arti Dec 30 '15 at 08:18