请求不能在python3中工作,只能在python2中工作

2024-04-20 02:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用请求来处理post请求,遇到了这样的情况:如果在python3中运行相同的代码,我会得到一个无效的响应,但是如果在python2中运行它,它就会工作!你知道吗

import requests

url = "https://creator.zoho.com/api/xml/write"

querystring = {"authtoken":"token"}

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"XMLString\"\r\n\r\n\n 
<ZohoCreator>
    <applicationlist> 

... content ...

   </applicationlist>
</ZohoCreator>\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"


headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'Content-Type': "application/xml",
    'cache-control': "no-cache",
    'Postman-Token': "03197e8c-2aef-4ac4-829d-f7dca06a14be",
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0'
    }

session = requests.Session()
response = session.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

Python 3响应:

{"code":2945,"message":"LESS_THAN_MIN_OCCURANCE"}

Python 2响应:

<response><result>
    ... content ...
<status>Success</status></add></form></result></response>

我肯定的要求是好的,因为它在邮递员工作,这是它生成的代码。说到python3,我是不是遗漏了什么?你知道吗


Tags: 代码formurldataresponsexmlcontentrequests
2条回答

我改变了结构和内容类型。现在它成功地发布了python3。你知道吗

payload = "XMLString=<ZohoCreator> ... </ZohoCreator>"

headers = {
    'Content-Type': "application/x-www-form-urlencoded",
    'cache-control': "no-cache",
}

我真的不知道它是如何与Python2。 但错误显示,这是由于无效票。请参阅下面的链接以生成api和PostUrl,将数据插入zoho creator。你知道吗

https://www.zoho.com/creator/help/api/prerequisites/generate-auth-token.html

https://www.zoho.com/creator/help/script/post-url.html#Example

相关问题 更多 >