如何在请求python中调用post api?

问题描述 投票:-1回答:2

我有一个采用三个参数的API,两个是字符串,而第三个是名为“ content”的文件。

from datetime import date

AuthToken = "xxxxxxxxxxx"
fileName = "a.txt"
FolderID = "xxxxxxxxxxx"
myfiles = {'file': ('report.xls', open('C:\\Users\\Nawaf Momin\\Desktop\\a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
URL = "https://apidocs.zoho.com/files/v1/upload?authtoken=" + AuthToken + "&scope=docsapi"
mydata = {
    'filename': fileName,
    'fid': FolderID,
    'content': myfiles
}
r = requests.post(url=URL, data=mydata)
print(r.status_code)
print(r.text)

有关api文档,请访问:https://www.zoho.com/docs/zoho-docs-api.html其上传帖子api ..

python post python-requests
2个回答
0
投票

您没有指出要获取的错误,所以我认为文件内容没有显示出来。您可能应该单独放置文件内容并向其添加标头,并且您还需要安装和导入请求库,因为它不是内置的。

from datetime import date
import requests

headers = {
"User-Agent":"Mozilla/5.0" #you will need to complete this user agent yourself
}

AuthToken = "xxxxxxxxxxx"
fileName = "a.txt"
FolderID = "xxxxxxxxxxx"
myfiles = {'file': ('report.xls', open('C:\\Users\\Nawaf Momin\\Desktop\\a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
URL = "https://apidocs.zoho.com/files/v1/upload

params = {
authtoken":AuthToken,
"scope":"docsapi"
}

r = requests.post(url=URL,  params=params, files=myfile, headers=headers)
print(r.status_code)
print(r.text)

希望这有所帮助


0
投票

我解决了。myfiles = {'file': ('report.xls', open('C:\\Users\\Nawaf Momin\\Desktop\\a.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}在这里,我发送的是parameter_name,而不是file。所以现在看起来像myfiles = {'parametername': ('report.xls', open('pathToFile', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

© www.soinside.com 2019 - 2024. All rights reserved.