0

I'm trying to post an order but below code remove spaces between words.

For example it post to the web side "StreetDistrictEtc" if customer_address is "Street District Etc"

I wonder what could be possible cause? Because of CFStringConvertEncodingToNSStringEncoding or something on web service side?

    let form1 = "user_id=\(self.order.user_id)&product_id=\(self.order.order_id)&order_no=\(self.order.order_no)&customer_addres=\(self.order.customer_address)"

    let myUrl = NSURL(string: "http://webservis.mydomain.com/order_post.asp")
    let request = NSMutableURLRequest(URL:myUrl!)
    request.HTTPMethod = "POST"

    let cfEnc = CFStringEncodings.ISOLatin5
    let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue))
    request.HTTPBody = form1.dataUsingEncoding(enc)
mehmeet43
  • 183
  • 15
  • Is there a reason why you are using ISOLatin5 and CFStrings? If it doesn't matter then maybe you could just use `request.HTTPBody = form1.dataUsingEncoding(NSUTF8StringEncoding)` for instance? – pbodsk Mar 02 '16 at 07:51
  • Because server side's unicode is ISOLatin5. Also I tried request.HTTPBody = form1.dataUsingEncoding(NSUTF8StringEncoding) but didn't work. – mehmeet43 Mar 02 '16 at 07:53
  • OK. How doesn't it work? Are spaces still removed in that case or what fails? – pbodsk Mar 02 '16 at 07:54
  • Two problems appear when I use NSUTF8StringEncoding; I got lost turkish characters and also spaces between words removed. – mehmeet43 Mar 02 '16 at 08:12
  • Weird indeed. If I try this in a playground `let string = "Street District Etc" let cfEnc = CFStringEncodings.ISOLatin5 let enc = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEnc.rawValue)) if let cfData = string.dataUsingEncoding(enc) { let decodedCFString = String(data: cfData, encoding: enc) }` then I end up with `decodedCFString` being "Street District Etc", as you would like and expect. So it must be something else causing this. Could it be because you try to add it as a HTTPBody maybe? Or has your `order.customer_address` already been changed before that maybe? – pbodsk Mar 02 '16 at 08:21
  • Have you made any progress? – pbodsk Mar 02 '16 at 12:18
  • Not really. I wonder whether if the problem cause from different encoding on app side and webservice. Are there another simple post method I can use instead of above? – mehmeet43 Mar 02 '16 at 13:03
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105144/discussion-between-pbodsk-and-mehmeet43). – pbodsk Mar 02 '16 at 13:11
  • 2
    @mehmeet43 The problem is that a space is a reserved character in an `application/x-www-form-urlencoded` request (which is what you're creating). So you have to percent escape this. See http://stackoverflow.com/a/25154803/1271826. – Rob Mar 03 '16 at 01:25
  • pbodsk and rob ; finally I solved the problem by using percent escape. thanks for your time. – mehmeet43 Mar 03 '16 at 08:15

0 Answers0