简体   繁体   English

Alamofire EmptyResponseCodes 不起作用

[英]Alamofire EmptyResponseCodes doesn't work

Endpoint responds with code 201 and an empty response body.端点以代码 201 和空响应正文进行响应。 Looking through the Alamofire documentation there is only 204 and 205 response body can be empty.查看 Alamofire 文档,只有 204 和 205 响应主体可以为空。 There is a solution that we can specify status code with empty results.有一个解决方案,我们可以指定带有空结果的状态代码。 Added set of添加了一组

emptyResponseCodes: [200, 201, 202, 203, 204, 205]

and after sending a request, I still get an error?= nil What I am doing wrong here?发送请求后,我仍然收到错误消息?= nil 我在这里做错了什么?

responseDecodable(of: TResult.self,
                  decoder: self.jsonDecoder,
                  emptyResponseCodes: [200, 201, 202, 203, 204, 205],
                  completionHandler: { (response: DataResponse<TResult, AFError>) in
                                    
                                    if let error = response.error {
                                        taskCompletionSource.set(error: error)
                                    } else if let result = response.value {
                                        taskCompletionSource.set(result: result)
                                    } else {
                                        taskCompletionSource.set(result: EmptyCodable())
                                    }

Alamofire includes an Empty type for exactly this purpose, as well as an EmptyResponse protocol so that types can define their own empty value. Alamofire 包含一个Empty类型,正是为了这个目的,以及一个EmptyResponse协议,这样类型就可以定义它们自己的空值。 In our included response serializers we check to see if an empty response is allowed, and, if so, attempt to cast the relevant empty type.在我们包含的响应序列化程序中,我们检查是否允许空响应,如果允许,则尝试转换相关的空类型。

For example:例如:

struct EmptyEntity: Codable, EmptyResponse {
    
    static func emptyValue() -> EmptyEntity {
        return EmptyEntity.init()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM