0
votes

I am trying to make a HTTP Post using alamofire, below is my code:

@IBAction func loginUserBtn(_ sender: Any) {

    let urlAddress = "http://xx.xx.xx.xx:xx/xx/xx/xx/login"
    let pass = Array(userPassword.text!)
    let username = userEmail.text!

    let params : [String: Any] = ["username": username, "pwd":  pass]
    print(params)

    let headers: HTTPHeaders = [
        "Accept": "application/json",
        "Content-Type": "application/json"
    ]


    Alamofire.request(urlAddress, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers).responseJSON{

        responds in
        if responds.result.isSuccess{
            print("Succesfull")

            let LoginJSON : JSON = JSON(responds.result.value!)
            //self.updateWeatheData(json: weatherJSON)
            print(LoginJSON)
        }
        else{
            print("Error \(String(describing: responds.result.error))")


        }
    }

}

This is the error I'm getting but if I change responseJSON to responseString I get a success, but the server is rejecting my data.

Here is my error message:

Error Optional(Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})))

1
The error message is saying that the response is not a valid JSON, so Alamofire cannot deserialize the response. Very probably the server is responding with some sort of errors, can be html. What do you get when you use responseString?OOPer
you can check your request response through the postman applicationNeha

1 Answers

0
votes
encoding: URLEncoding.default

I see what you have header of content-type is JSON, but Encoding field inform in what type your params. Try this:

encoding: JSONEncoding.default