-1

When when I use a POST using AlamoFire one of the String param's has added characters to it? Any idea?

let email = emailText.text

Alamofire.request("https://xxxx", method: .post, parameters: ["subscribed": true,"address": email],encoding: URLEncoding.httpBody, headers: headers).validate()
        .log().responseJSON {
        response in

This is what the params look like

address=Optional%28%22Adam%40yahoo.com%22%29&subscribed=1

This is what it looks like if I hard code the email to

let email = "adam@yahoo.com"

Result

address=adam%40yahoo.com&subscribed=1
user1163234
  • 2,407
  • 6
  • 35
  • 63

1 Answers1

0

The problem is your email is wrapped into an optional (nullable value). You might want to unwrap it first:

guard let email = emailText.text else {
  // Handle the case where there is no email
  return
}
...
alanpaivaa
  • 1,959
  • 1
  • 14
  • 23
  • `"Handle the case where there is no email"` The else conditional of the guard will never be executed. Better to use `emailText.text!` directly. The UITextField text property default value is an empty string. Even if you assign nil to it it will return an empty string right after it. – Leo Dabus Mar 05 '19 at 01:54
  • Not quite actually. You should be very careful about using `!`, since it will crash if it's nil. This may not be the case, but keep this in mind for the future. – alanpaivaa Mar 05 '19 at 01:56
  • You can test it yourself – Leo Dabus Mar 05 '19 at 01:57
  • Btw you can use `UIKeyInput` property called `hasText` which is made exactly for this. `if emailText.hasText {` https://developer.apple.com/documentation/uikit/uikeyinput/1614457-hastext – Leo Dabus Mar 05 '19 at 01:57