Alamofire: Responseserializationfailed(Alamofire.Aferror.Responseserializationfailurereason.Inputdatanilorzerolength)

error: inputDataNilOrZeroLength in Swift with Alamofire

For me was right:

let authRequest = AF.request(url,
method: .post,
parameters: parameters,
encoding: JSONEncoding.default)
authRequest.responseString { response in
switch response.result {
case .success(let value):
print("succes")
case .failure(let error):
print("Error while querying database: \(String(describing: error))")
}
}

JSONEncoding + responseString(Just for more convenience)

responseSerializationFailed with Alamofire

Try this

Paramters:

let parameters: [String: Any] = [
"jsonrpc":"2.0",
"method":"call",
"params": [
"db": db,
"login": "mohanad@amana‌​.ps",
"password": "moh‌​anad123",
]
]

request. use a encoding: JSONEncoding.default, because you have pass Raw value.

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil)
.validate(statusCode: 200..<600)
.responseJSON { response in

switch response.result
{
case .failure(let error):
if let data = response.data {
print("Print Server Error: " + String(data: data, encoding: String.Encoding.utf8)!)
}
print(error)

case .success(let value):

print(value)
}

}

your api give me this type of data in this

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>Session expired (invalid CSRF token)</p>\n"

Alamofire responseSerializationFailed

I managed to fix the issue after a couple of days of searching and debugging. When entering the URL for the Alamofire request I was using the URL formatted like https:///www.test.com. I added an extra slash at the end as follows https:///www.test.com/

According to what I found, the request will get redirected to the correct URL and the headers are not sent along with the request. Thus explaining the 401 response (Unauthorized)



Related Topics



Leave a reply



Submit