0

I have an issue of not understanding when to encode a URL OR to just replace occurrences of a char. The app has worked fine and doing an upgrade to swift 3 where the server now gives a response of INVALID USER ACCOUNT.

Here is the code:

userEmail: String = "myEmail+a@gmail.com"?    
let body : String = ("username=\(userEmail!)&password=\("thePassword")")
var request = NSMutableURLRequest()
request = NSMutableURLRequest(url: URL(string:"https://team.mycompany.com/teambeta/LoginApp")!)
request.httpMethod = "POST"
request.httpBody = body.data(using: String.Encoding.utf8)

So the email in question includes a "+" as I need to create multiple user accounts for testing all with a unique email address. SO I am using a single Gmail account and add the "+" so all emails go to the single email address. Users who know about the "+" feature may create user accounts too so I need to solve this not just for testing purposes.

myEmail@gmail.com
myEmail+a@gmail.com
myEmail+b@gmail.com

So my question is, if I just do:

request.httpBody = body.data(using: String.Encoding.utf8)

will i get the body changed to:

username=myEmail%2ba@gmail.com&password=thePassword

OR do I need to first of all do a replace text like so:

            userEmail = userEmail?.replacingOccurrences(of: "+", with: "%2b")

and if I just do the replace text do I then not have to encode.

Sorry if its a duplicate question as I have not found the answer yet in other questions on SO.

timv
  • 3,346
  • 4
  • 34
  • 43
  • Take a look at the following SO thread where this is discussed in detail - http://stackoverflow.com/questions/6603928/should-i-url-encode-post-data – Fahim Mar 23 '17 at 00:03
  • Possible duplicate of [Should I URL-encode POST data?](http://stackoverflow.com/questions/6603928/should-i-url-encode-post-data) – Fahim Mar 23 '17 at 00:03

0 Answers0