Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading file on sharepoint with python causes uploaded file to retain headers in the content

When I submit the post request using my script, I do not encode the data and prepare the post request as such:

postheader.update({'Accept':'application/json; odata=verbose','Content-Type':'application/json; odata=verbose', 'X-RequestDigest':formdigestvalue, 'binaryStringRequestBody':'true'})

filetoupload = {'file':(filename, open(filename, 'rb'), 'application/vnd.openxmlformats-officedocument.presentationml.presentation')}
posturl = projectConfig.sharepointurl + "/_api/web/getfolderbyserverrelativeurl('Shared Documents/release_doc/"+project+"')/files/add(url='"+filename+"', overwrite='true')"
response = requests.post(posturl, headers=postheader, files=filetoupload, cookies=postcookies)

I took a look at the file that I uploaded using my script and it has the following headers still attached in the content:

--6ab0a06f1ddc432186194dd48355eac1
Content-Disposition: form-data; name="ZZZ Technology Guide.pptx"; filename="ZZZ Technology Guide.pptx"
Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation

<file content>
--6ab0a06f1ddc432186194dd48355eac1--

However the file that was uploaded through firefox did not have these headers. I'm thinking I need to call a sharepoint function (if there is a REST API that does that) to process the file after it is uploaded or if there's a method in the python requests that can dispose off these headers as mentioned in this post: stackoverflow post

like image 902
emad Avatar asked Nov 18 '16 18:11

emad


1 Answers

Do NOT use multipart/form-data i.e. in the curl world, use "--data-binary "@testFile.xls" instead of -F upload="@testFile.xls"

like image 100
Henry Avatar answered Oct 18 '22 13:10

Henry