英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论