0

I am current using the following to try and create an upload session with the onedrive api

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers["Authorization"] = "Bearer " + nODUserTokenObj.Access_Token;
request.ContentType = "application/json";
string data =
        @"{
            ""@microsoft.graph.conflictBehavior"": ""replace""
        }";
byte[] postBytes = Encoding.ASCII.GetBytes(data);
request.ContentLength = postBytes.Length;
request.Accept = "application/json";

request.GetRequestStream().Write(postBytes, 0, postBytes.Length);

using (HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse())
{
    using(StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        string theData = sr.ReadToEnd();

        JObject test = JObject.Parse(theData);
    }
}

Currently I am receiving an exception stating

The remote server returned an error: (409) Conflict.

However looking at the Microsoft documentation I should have a json object returned to me with more information.

What am I doing wrong that I am getting an exception thrown when it hits (HttpWebResponse)request.GetResponse() as opposed to receiving a json object with more error details?

peroija
  • 1,982
  • 4
  • 21
  • 37
  • [The parameter @microsoft.graph.conflictBehavior should be included in the URL instead of the body of the request.](https://learn.microsoft.com/en-us/graph/api/resources/driveitem?view=graph-rest-1.0#instance-attributes). – Crowcoder May 04 '22 at 12:22
  • @Crowcoder I guess this is the fun of Microsoft because for creating an upload session as I am trying to do here they say in the body: https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online – peroija May 04 '22 at 12:34

1 Answers1

0

I came across two solutions:

  1. I initially resolved this by sending the request through Fiddler and reading the response there. This was fine for my situation as I was developing and just trying to figure out how the onedrive api functioned.
  2. A solution going forward would be to follow something like this post suggests (How to get error information when HttpWebRequest.GetResponse() fails)

To summarize the post:

catch (WebException ex)
{
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        Console.WriteLine(reader.ReadToEnd());
    }
}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached
}
peroija
  • 1,982
  • 4
  • 21
  • 37