Alamofire: status code: 404,but response.result = SUCCESS

Created on 27 Oct 2015  ·  3Comments  ·  Source: Alamofire/Alamofire

1

response serializer support

Most helpful comment

You aren't validating the response status code, so it is possible that a 404 will return you a .Success case. All that the responseData serializer does is validate that there wasn't an error (which there won't be since you aren't validating the response status code) or that the data exists.

public static func dataResponseSerializer() -> ResponseSerializer<NSData, NSError> {
    return ResponseSerializer { _, _, data, error in
        guard error == nil else { return .Failure(error!) }

        guard let validData = data where validData.length > 0 else {
            let failureReason = "Data could not be serialized. Input data was nil or zero length."
            let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }

        return .Success(validData)
    }
}

In your case, you are receiving a 404 which isn't throwing an error, and the server is returning data. Therefore, your responseData serializer succeeds. If you expect a 404 to always throw an error, you should additionally use the .validate(statusCode: [200]) method before the responseData serializer. More info in the Validation Section of the README.

In the future, questions like this are better suited for Stack Overflow with an alamofire tag. We use GitHub for bugs and feature requests. 🍻


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

All 3 comments

You aren't validating the response status code, so it is possible that a 404 will return you a .Success case. All that the responseData serializer does is validate that there wasn't an error (which there won't be since you aren't validating the response status code) or that the data exists.

public static func dataResponseSerializer() -> ResponseSerializer<NSData, NSError> {
    return ResponseSerializer { _, _, data, error in
        guard error == nil else { return .Failure(error!) }

        guard let validData = data where validData.length > 0 else {
            let failureReason = "Data could not be serialized. Input data was nil or zero length."
            let error = Error.errorWithCode(.DataSerializationFailed, failureReason: failureReason)
            return .Failure(error)
        }

        return .Success(validData)
    }
}

In your case, you are receiving a 404 which isn't throwing an error, and the server is returning data. Therefore, your responseData serializer succeeds. If you expect a 404 to always throw an error, you should additionally use the .validate(statusCode: [200]) method before the responseData serializer. More info in the Validation Section of the README.

In the future, questions like this are better suited for Stack Overflow with an alamofire tag. We use GitHub for bugs and feature requests. 🍻


From our Contribution Guidelines

Asking Questions

We don't use GitHub as a support forum. For any usage questions that are not specific to the project itself, please ask on Stack Overflow instead. By doing so, you'll be more likely to quickly solve your problem, and you'll allow anyone else with the same question to find the answer. This also allows maintainers to focus on improving the project for others.

I really appreciate

I am struggling with this issue... Thanks you so much.

Was this page helpful?
0 / 5 - 0 ratings