ygetarts
ygetarts

Reputation: 944

c# Trouble with PUT request using Skydrive REST API

I'm trying to upload a file to SkyDrive via the REST API. I've been using the following code, but I keep getting a "(415) Unsupported Media Type." error:

            var requestUriFile =
                             new StringBuilder("https://apis.live.net/v5.0/<folderid>/files/testfile.txt");
        requestUriFile.AppendFormat("?access_token={0}", accessTokenM);

        byte[] arr = System.IO.File.ReadAllBytes("C:\\temp\\testFile.txt");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestUriFile.ToString());
        request.Method = "PUT";
        request.ContentType = "text/plain";
        request.ContentLength = arr.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(arr, 0, arr.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        string returnString = response.StatusCode.ToString();

I've also tried using RestSharp to do this, but I'm running into a similar issue. When I run the below code, I get returned the exception "The provided Content-Type header 'multipart/form-data; boundary\u003d-----------------------------28947758029299' is not supported".

            byte[] arr = System.IO.File.ReadAllBytes("C:\\temp\\testFile.txt");

        var client = new RestClient("https://apis.live.net/v5.0/");
        var request = new RestRequest(Method.PUT);
        request.Resource = "<folderId>/files/testfile.txt?access_token=" + accessTokenM;
        request.AddHeader("content-type", "text/plain;");
        request.AddFile("filename", arr, "testfile.txt", "text/plain");



        var responseIn = client.Execute(request);

What am I doing wrong here?

Upvotes: 1

Views: 1325

Answers (1)

ygetarts
ygetarts

Reputation: 944

Ok, I solved it. Apparently all I need to do is leave the ContentType blank and it works. Thanks :)

Upvotes: 5

Related Questions