Alamofire.AFError.ResponseValidationFailureReason.unacceptableStatusCode(500)
Asked Answered
G

5

11

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!

Greff answered 18/1, 2017 at 5:56 Comment(0)
C
8

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.

Cantwell answered 19/1, 2017 at 4:59 Comment(2)
I figured out that my problem was with the server but thanks man!Greff
@Greff what issue you faced on the server side, I am also getting the exact problemFayre
W
4

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.

Wensleydale answered 31/5, 2017 at 2:26 Comment(0)
S
2

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.

Seamy answered 24/3, 2023 at 15:50 Comment(0)
W
0

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

Whitebait answered 20/7, 2018 at 16:40 Comment(0)
G
0

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'
Glazer answered 16/6, 2020 at 7:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.