如何修复API请求返回426。

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

how to fix api request returns 426

问题

I have a problem that when I send a request using requests for my api, I receive a 426 response which means: "protocol Upgrade Required."

My code:

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}

req = requests.get(myApi, headers=headers, auth=user_name_password)
print(req.status_code)

the result:
426

When I try to put the API on the browser, I give the username and password and get the required result. So, I think that the API needs to be updated.

When I copy as curl, I take all of the headers and try to put them in my headers, but again, I have a 426 error.

英文:

I have a problem that when I send a request using requests for my api , I receive a 426 response which means: "protocol Upgrade Required"
My code:

headers= {
'user-agent ': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}


req = requests.get(myApi, headers=headers, auth=user_name_password)
print(req.status_code)

the result:
426


When I try to put the api on the browser , i give the username and password have a required result
So, I think that it is the api needs to be updated.

when I copy as curl , I take all of the header and try to put in my headers but again I have 426 error.

答案1

得分: 1

> 所以,我认为需要更新的是 API。

不,这不是426的意思,它是:

> 客户端错误响应代码表示服务器拒绝使用当前协议执行请求,但在客户端升级到不同协议后可能愿意执行请求。

所以 服务器 正在通知 客户端 需要升级,详细信息在 Upgrade 响应头中给出,要查看其值,请在代码的最后一行之后添加:

print(r.headers['Upgrade'])
英文:

> So, I think that it is the api needs to be updated.

No this is not what 426 means, it is

> client error response code indicates that the server refuses to perform the request using the current protocol but might be willing to do so after the client upgrades to a different protocol.

So server is informing that client needs upgrade, details are given in Upgrade response header, to reveal its' value add

print(r.headers['Upgrade'])

after last line of your code

huangapple
  • 本文由 发表于 2023年5月15日 00:04:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76248446.html
匿名

发表评论

匿名网友

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

确定