客户端执行时,错误会返回状态码吗?

huangapple go评论120阅读模式
英文:

client.Do errors get status code

问题

当我调用client.Do方法时,如何通知我出现429响应代码(也称为太多请求)的错误。我应该期望得到一个带有状态码429的响应对象,还是只会得到一个带有消息"too many requests"的错误?

我看到的是后者,我需要能够获取所有错误(服务器/拨号/ TCP / DNS等)和成功的状态码,只有在某些情况下才能获取实际的错误代码。是否有办法获取所有响应代码,而不考虑错误类型 - 错误消息似乎反映了详细信息,但如果可能的话,仍然希望获取代码。

英文:

When I call client.Do method, how would I be notified for errors like 429 response code (aka too many requests). Should I expect to get a response object back with a status code 429 or will I get an error with just the message “too many requests”?

I am seeing the latter and what I need is to be able to get status code for all errors (server/dial/tcp/dns/etc.) and successes and only in some cases I can get the actual code in case of errors. Is there anyway to get all response codes irrespective of the error type - the error message seems to reflect the details but would still like to get the code if possible.

答案1

得分: 2

http文档非常清楚地介绍了这一点。

特别值得注意的是Do的文档

> 如果由客户端策略(例如CheckRedirect)引起错误,或者由于无法进行HTTP通信(例如网络连接问题)而引起错误,则返回错误。非2xx状态码不会引发错误。

(强调部分)

以及由Do返回的Response对象的文档:

type Response struct {
    Status     string // 例如"200 OK"
    StatusCode int    // 例如200
英文:

The http documentation covers this very clearly.

Of particular interest are the documentation for Do:

> An error is returned if caused by client policy (such as CheckRedirect), or failure to speak HTTP (such as a network connectivity problem). A non-2xx status code doesn't cause an error.

(Emphasis added)

And the documentation for the Response object returned by Do:

type Response struct {
    Status     string // e.g. "200 OK"
    StatusCode int    // e.g. 200

huangapple
  • 本文由 发表于 2017年4月14日 05:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/43401725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定