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.
The "type" of the file is determined by the
Content-Type
header. For some context, theAccept
header states the format you expect the response body to use while theContent-Type
states the format of your request.To upload a standard text file, you'll want to use
Content-Type: text/plain
: