简体   繁体   English

如何解决 HTTP Post 请求 Python 中的状态 401?

[英]How to Resolve Status 401 in HTTP Post Request Python?

I am trying to make an http post request in Python.我正在尝试在 Python 中发出 http post 请求。 The http that I am posting to requires an authorization.我发布到的 http 需要授权。 For security, I have replaced the real url, data and authorization.为了安全起见,我已经替换了真实的 url、数据和授权。 When I run the code below with the correct url, data body and authorization I get status 401. That would imply that the authorization is not correct.当我使用正确的 url、数据主体和授权运行下面的代码时,我得到状态 401。这意味着授权不正确。 Is there another way I should be adding authorization, headers and body to my http post request so that the post is successful?是否有另一种方法应该向我的 http 发布请求添加授权、标头和正文,以便发布成功? The authorization password is correct and works in Postman, so it's contents is not the issue--I'm wondering if the format is.授权密码是正确的并且可以在 Postman 中使用,所以它的内容不是问题——我想知道格式是否正确。 Thanks in advance!提前致谢!

import requests
from requests.auth import HTTPBasicAuth
    
    def post():
            url = 'http://somelongurl'
            headers = {'Content-Type': 'application/json'}
            auth = HTTPBasicAuth('Authorization', 'PASSWORD')
            data = {"ProductID" : "ABCDEFG",
                    "ClientID": "10000",
                    "SerialNumber": "1234567890"}
            req = requests.post(url, headers=headers, data=data, auth=auth)
            return req

这是通过在标题中包含授权并将数据更改为 json 类型来解决的,如下所示: https : //postimg.cc/3y7B6fvL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM