1

I am trying to upload a file to a SharePoint Drive by using Microsoft Graph. I am new to REST APIs and Microsoft Graph.

This is what the documentation says:

PUT /me/drive/root:/FolderA/FileB.txt:/content
Content-Type: text/plain
The contents of the file goes here.

Before all of this, I do have my authorization/bearer token and I am able to call the HTTP get but I am not able to upload the file using HTTP PPU.

URL url = new URL(newUrl);
String readLine;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization","Bearer "+ token);
connection.setRequestProperty("Accept","application/json");

This returns java.io.IOException: Server returned HTTP response code: 411 for URL.

I have tried passing it as a binary stream but the request is still failing.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Pujan Tandukar
  • 41
  • 1
  • 1
  • 3
  • Please include the file size and the error you're receiving, "not able to upload" is not specific enough to go. – Marc LaFleur Apr 26 '21 at 21:19
  • Sorry about that Marc, the file size is pretty low, its less than 4mb for sure. For example I am using a text file thats only couple hundred bytes. I am stuck at how to pass in the content as the body as a part of the request. When I make the request I get java.io.IOException: Server returned HTTP response code: 411 for URL – Pujan Tandukar Apr 27 '21 at 03:56

1 Answers1

0

The "type" of the file is determined by the Content-Type header. For some context, the Accept header states the format you expect the response body to use while the Content-Type states the format of your request.

To upload a standard text file, you'll want to use Content-Type: text/plain:

URL url = new URL(newUrl);
String readLine;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization","Bearer "+ token);
connection.setRequestProperty("Accept","application/json");
connection.setRequestProperty("Content-Type","text/plain");
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Since it's text/plain, would binary data need to be base64-encoded first? The docs don't mention it, but it wouldn't be the first time the docs have missed something – Hong Ooi Apr 27 '21 at 13:38
  • This won't work because the Graph can't decode the base-64 on the other end. You'll want to send a byte array instead. My Java is [extremely] rusty but in C# you would use something like `byte[] data = System.IO.File.ReadAllBytes(path);`. – Marc LaFleur Apr 27 '21 at 13:53
  • Yeah, I'm just suspicious because the other endpoints I've seen that take text/plain data, require it to be base64 encoded first. In particular, the Outlook "small attachment" endpoint has this requirement https://learn.microsoft.com/en-us/graph/api/message-post-attachments?view=graph-rest-beta&tabs=http – Hong Ooi Apr 27 '21 at 14:08
  • @MarcLaFleur Thanks for the response back. I do have the content type header and I also do have the accept header state. My question is how can we add the file into the request? So, we define the file and convert it into a byte array but how do we pass that into the request? – Pujan Tandukar Apr 27 '21 at 14:52
  • You provide a byte stream in the request body. Take a look at this answer, it might help https://stackoverflow.com/questions/61954090/java-update-a-docx-file-on-sharepoint-online-with-graph-api – Marc LaFleur Apr 27 '21 at 16:50