Swift how to send symbols like "&(*(" with HTTP post -


i have got swift code

    var request = urlrequest(url: url(string: "http://www.centill.com/ajax/logreg.php")!)     request.httpmethod = "post"     let poststring = "app_log_pass=\(pass_text_field.text!)"     request.httpbody = poststring.data(using: .utf8) 

and when text (1&*^&&2 prints (1.how can send string contains various symbols safely swift?

as sulthan suggested, no predefined characterset can used actual servers when want include symbol characters in post data.

an example characterset use:

extension characterset {     static let rfc3986unreserved = characterset(charactersin:         "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-._~") }  let postvalue = "(1&*^&&2" let poststring = "app_log_pass=\(postvalue.addingpercentencoding(withallowedcharacters: .rfc3986unreserved)!)" print(poststring) //->app_log_pass=%281%26%2a%5e%26%262 

another characterset work usual php servers:

extension characterset {     static let queryvalueallowed = characterset.urlqueryallowed.subtracting(characterset(charactersin: "&+=")) }  let postvalue = "(1&*^&&2" let poststring = "app_log_pass=\(postvalue.addingpercentencoding(withallowedcharacters: .queryvalueallowed)!)" print(poststring) //->app_log_pass=(1%26*%5e%26%262 

anyway, need escape key , value separately, prevent escaping separator =.


addition

an example of escaping multiple key-value pairs:

extension string {     var queryvalueescaped: string {         return self.addingpercentencoding(withallowedcharacters: .queryvalueallowed)!     } }  let params: [string: string] = [     "app_log_usn": "ooper",//usn_text_field.text!,     "app_log_pass": "(1&*^&&2"//pass_text‌​_field.text! ] let poststring = params.map {"\($0.key)=\($0.value.queryvalueescaped)"}.joined(separator: "&") print(poststring) //->app_log_usn=ooper&app_log_pass=(1%26*%5e%26%262 

assuming each key made of safe characters , no need escape.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -