Alamofire 2在响应中给出错误的HTTP状态码时仍能成功发起请求

3

现在我已经升级到Alamofire 2,我注意到即使返回非200或4xx的http响应,回调函数也认为响应是成功的。

这是预期的行为吗?如何检查错误响应 - 只需手动检查response.statusCode吗?

Alamofire.request(.GET, "http://somesite.org/private")
         .responseJSON { _, _, result in
             print(result.isSuccess) // is true even if it's a 403 or 404 response
         }
1个回答

3

我认为这是默认行为。你需要验证响应。如果响应状态码不在提供的范围内,那么所有操作都将被视为失败。

Alamofire.request(.GET, "http://somesite.org/private")
     .validate(statusCode: 200..<300)
     .responseJSON { _, _, result in
         print(result.isSuccess) // is true even if it's a 403 or 404 response
     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接