使用API Postman将文件上传到SharePoint

问题描述 投票:0回答:2

我想使用Rest API通过Oracle系统将文件上传到SharePoint。 我尝试了以下方法,但没有成功。

控制台:

发帖

https://test.sharepoint.com/sites/BusinessDevelopment/Shared%20Documents/Upload_BMT/_api/web/GetFolderByServerRelativeUrl('P_FIRST')/Files/add(url='abdabd.pdf',overwrite=true) :

“请求标头”:{

"content-length": "100000",
"authorization": "Bearer access token",
"user-agent": "PostmanRuntime/7.28.4",
"accept": "*/*",
"postman-token": "11",
"host": "test.sharepoint.com",
"accept-encoding": "gzip, deflate, br",
"connection": "keep-alive",
"content-type": "multipart/form-data; boundary=--------------------------327461050037851785007682"

},

“请求正文”:{ “”:“” },

“响应标头”:{

"cache-control": "private, max-age=0",
"expires": "Mon, 20 Dec 2021 05:18:53 GMT",
"last-modified": "Tue, 04 Jan 2022 05:18:53 GMT",
"vary": "Origin",
"p3p": "CP=\"ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI\"",
"x-sharepointhealthscore": "2",
"x-aspnet-version": "4.0.30319",
"sprequestguid": "375b13a0-70ce-c000-bacc-4fdfeefc7910",
"request-id": "375b13a0-70ce-c000-bacc-4fdfeefc7910",
"ms-cv": "oBNbN85wAMC6zE/f7vx5EA.0",
"strict-transport-security": "max-age=31536000",
"x-frame-options": "SAMEORIGIN",
"content-security-policy": "frame-ancestors 'self' teams.microsoft.com *.teams.microsoft.com *.skype.com *.teams.microsoft.us local.teams.office.com *.powerapps.com *.yammer.com *.officeapps.live.com *.office.com *.stream.azure-test.net *.microsoftstream.com *.dynamics.com;",
"x-powered-by": "ASP.NET",
"microsoftsharepointteamservices": "16.0.0.21924",
"x-content-type-options": "nosniff",
"x-ms-invokeapp": "1; RequireReadOnly",
"x-cache": "CONFIG_NOCACHE",
"x-msedge-ref": "Ref A: 3C3309D8FC4C43CAA72F28D56F17244B Ref B: ZRHEDGE1219 Ref C: 2022-01-04T05:18:53Z",
"date": "Tue, 04 Jan 2022 05:18:53 GMT",
"content-length": "0"
sharepoint postman sharepoint-online sharepoint-api
2个回答
3
投票

网址应如下所示

网址:

http://test.sharepoint.com/sites/testsite/_api/web/GetFolderByServerRelativeUrl('/Library Name/Folder Name')/Files/add(url='a.txt',overwrite=true)


0
投票

只是添加另一个答案,以防万一有人在寻找它。因为我们没有太多可用结果。

以下方法是使用Python脚本将Excel文件上传到SharePoint。相同的数据也可以在邮递员中使用。

base_url_with_sitename = "https://abcportal.sharepoint.com/sites/my_site_name
site_location = "sites/my_site_name/Shared Documents/General/Project
my_date_folder = "12202024"
filename = "Sample.xlsx"

rest_api_url = f"https://{base_url_with_sitename}/_api/web/GetFolderByServerRelativeUrl('/{site_location}/{my_date_folder}')/Files/add(url='{filename}', overwrite=true)"

现在读取 Excel 文件并将其内容作为二进制数据存储在变量中。

with open(filename, "rb") as mf:
    file_content = mf.read()

headers = {
        "Content-Type": "application/json",
        "Content-Length": str(len(file_content)),
        "Accept": "application/json;odata=verbose",
        "Authorization": f"Bearer <mytoken>",
        "X-RequestDigest": <formdigestval>
    }

resp = requests.post(rest_api_url, headers=headers, data=file_content)

if resp.status_code == 200:
    print("File Uploaded Successfully")
else:
    print("File Upload Failed")
© www.soinside.com 2019 - 2024. All rights reserved.