0

I've been combing through Microsoft's documentation on the REST API to manipulate files in our SharePoint Online document tree. I'm ALMOST to my solution but having an issue.

I'm running CURL commands on a Linux server. And in the /tmp directory on Linux I have a file 'a.txt'. I can create a new file on SharePoint online using CURL, but I cannot upload an existing file using the same command.

When I specify "Content-Length: 0", this command successfully creates a new file on our SharePoint

curl -X POST -i -H "Authorization: Bearer tokenabc" -H "Content-Length: 0" -s "https://tenant.sharepoint.com/teams/testSite/_api/web/GetFolderByServerRelativeUrl('%20Shared%20Documents')/Files/add(url='a.txt',overwrite=true)"

But when I try to actually UPLOAD my file from the /tmp directory, (I run the CURL command in the /tmp directory, I change "Content-Length: 4000"), I get no response whatsoever and no file is created.

curl -X POST -i -H "Authorization: Bearer tokenabc" -H "Content-Length: 4000" -s "https://tenant.sharepoint.com/teams/testSite/_api/web/GetFolderByServerRelativeUrl('%20Shared%20Documents')/Files/add(url='a.txt',overwrite=true)"

I cannot seem to figure out how to upload my file from the Linux server itself using this CURL REST API command

2 Answers2

0

You can try using the graph API to upload files to SharePoint Online.

PUT /sites/{site-id}/drive/items/{parent-id}:/{filename}:/content
Content-Type: text/plain

The contents of the file goes here.
Carl Zhao
  • 8,543
  • 2
  • 11
  • 19
0

I found out I was using the CURL command wrong, I needed to use the -F option to specify I was using an actual file on the machine.

Once I included the -F option, it worked

curl -F "file=@a.txt" "https://tenant.sharepoint.com/teams/testSite/_api/web/GetFolderByServerRelativeUrl('%20Shared%20Documents')/Files/add(url='a.txt',overwrite=true)" -H "Authorization: Bearer $(cat token.txt)" -H "accept: application/json"
Tyler2P
  • 2,324
  • 26
  • 22
  • 31