Question

According to the following link:

Working with folders and files with REST

I can add a file using

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files/add(url='a.txt',overwrite=true)
method: POST
body: "Contents of file"
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    content-length:length of post body

I am testing this with Postman. In my post request I set the body to form-data and add a file parameter with a text file called "a.txt". The text file contains the text:

one
two
three

When I execute my request in Postman my text file is present in SharePoint. When I open the file on SharePoint the contents of the file is:

----------------------------235004993628819232914336
Content-Disposition: form-data; name="file"; filename="a.txt"
Content-Type: text/plain

one
two
three
----------------------------235004

I want to do this action by only using plain HTTP calls. Is this behaviour correct? It looks like a file is created and the content is filled with my formdata.

Was it helpful?

Solution

Setting the body to binary in Postman fixed my problem. Key is setting the correct content-length otherwise the file will get corrupted.

Get the content length of thee file with PowerShell

$file = "c:\temp\file.docx"
(Get-Item $file).Length
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top