JoshJoshJosh
JoshJoshJosh

Reputation: 917

Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500)

I'm working on Alamofire and I am trying to post a request to the server like this:

func sendRequest () {

    print("fire now----------------------------------------------")

    let parameters: Parameters = ["user": "001", "name": "josh"]

    print(parameters)
   let a = Alamofire.request("http://120.77.252.96:8388/", method: .get, parameters: parameters, encoding: URLEncoding.default).validate(statusCode: 200..<500).responseJSON(completionHandler: {responds in
        switch responds.result {
        case .success(let value):
            let json = JSON(value)
            print("JSON: \(json)")
        case .failure(let error):
            print(error)
        }}
    )
    print(a)

}

But I constantly get errors like this:

Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500)

and when I check the server side, it seems that the body of the request is empty.
Can anyone help me with this? Thanks so much!

Upvotes: 11

Views: 25179

Answers (5)

Pratima
Pratima

Reputation: 51

Changing Status code to validate(statusCode: 200..<600) will work. But its not the correct way to fix. You can check with backend team and ask them they should send successful response under statuscode 200..<300.

Upvotes: 2

brockhampton
brockhampton

Reputation: 314

Tweaked my alamofire version in my pod file and it fixed the issue.

Try downgrading it in your pod file using:

pod 'Alamofire', ~>'4.8.2'

Upvotes: 0

vidalbenjoe
vidalbenjoe

Reputation: 975

Try to remove request.validate(statusCode: 200..<300)

Upvotes: 0

eyeezzi
eyeezzi

Reputation: 635

An explanation of Cyril's accepted answer:

HTTP server functions conventionally return a status code that indicates what happened while processing the request. Alamofire can read this to determine if the response is valid, or an error occured. Depending on how your server was implemented, you can tell Alamofire the range of status codes that you consider to mean a 'valid' response, you do this by giving this range to the validate() function. For example, .validate(statusCode: 200..<500) tells Alamofire that any response with the status code 200 up to 499 should be considered valid, every other code (including 500) should be invalid.

Upvotes: 4

Cyril
Cyril

Reputation: 2818

I had the same problem my friend and I resolved it by changing the status code from

validate(statusCode: 200..<500)

to

validate(statusCode: 200..<600)

I'm new to Alamofire so I cannot give you an explanation to why or how it works or what the error means.

Upvotes: 8

Related Questions