如何正确处理反映错误但状态码为200的响应?

时间:2019-09-10 09:44:48

标签: rest http http-status-code-404

向苦涩的跟踪器发出请求时,我得到以下响应:

    let config = URLSessionConfiguration.default
    config.httpCookieAcceptPolicy = .always
    config.httpShouldSetCookies = true
    config.httpCookieStorage = HTTPCookieStorage.shared
    let mySession = URLSession(configuration: config, delegate: self, delegateQueue: nil)

其状态码为 if let cookies = HTTPCookieStorage.shared.cookies(for: responseURL) { print(cookies) } 。我不知道这是否“正确”,但是它肯定不会很快改变。

我的问题是,当状态代码不能反映故障响应时,最好的方法是什么?我首先要做的是

HTTP/1.1 200 Not Authorized
Connection: Keep-Alive
Content-Encoding: gzip
Content-Length: 95
Content-Type: text/plain
Date: Tue, 10 Sep 2019 10:23:29 GMT
Keep-Alive: timeout=5, max=100
Pragma: no-cache
Server: Apache/2.4.18 (Ubuntu)
Vary: Accept-Encoding

d14:failure reason63:Requested download is not authorized for use with this tracker.e

这感觉很hack。处理此类回复的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

id

哦,太醉了。

  

处理此类回复的正确方法是什么?

看看有效载荷。

在HTTP中,“真实”消息位于有效负载中。标头和状态行是元数据,它们以通用 HTTP兼容代码可以理解的标准化方式描述有效负载。

您可能希望查看是否有其他可用的媒体类型。 200 Not Authorized 对于机器2的机器通信很糟糕。您宁愿拥有更易于解析的内容。

在理想的世界中,您可能可以要求诸如application/problem+json之类的东西,以便机器可以提取有关正在发生的事情的信息。当然,这也不会很快改变。