0
votes

Hey im trying to post an http request (Create Teams online meeting) to the Graph API under c# .Net Framework 4.0 so i cannot use the Graph-SDK and i cannot use the System.net.http libary (no httpClient). I tried HttpWebRequest but im having difficulties with posting Json to the API.

My Request Function looks like this:

private string SendHttpRequest(string Method, string ContentType, WebHeaderCollection Headers, string Content, string URI)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);

                request.Method = Method;
                request.Headers = Headers;
                request.PreAuthenticate = true;
                request.ContentType = ContentType;

                StreamWriter requestWriter = new StreamWriter(request.GetRequestStream());
                requestWriter.Write(Content);
                requestWriter.Close();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                var responseStream = response.GetResponseStream();

                StreamReader responseReader = new StreamReader(responseStream);
                string responseText = responseReader.ReadToEnd();
                responseReader.Close();

                return responseText;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

And this works fine when my Content Type is text/xml(for a diffrent API) but not if its Application/Json. I always get an Error 400 when i call HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Method = "POST"

ContentType = "application/json"

Headers = Headers.Set(HttpRequestHeader.Authorization, "Bearer " + accessToken);

Content = string jsonContent = JsonConvert.SerializeObject(Content);

URI = "https://graph.microsoft.com/v1.0/me/onlineMeetings"

1
What does the error specifically say? I remember Graph errors contain some additional information. - jjczopek
Which .net version you are using? - Trinetra-MSFT
@jjczopek The Complete Error im getting is: Der Remoteserver hat einen Fehler zurückgegeben: (400) Ungültige Anforderung. This is German and simply says Bad Request. im sadly not getting more information - Marvin Schröck
@Trinetra-MSFT .Net Framework 4 - Marvin Schröck
Can you share the JSON payload you are sending? - jjczopek

1 Answers

0
votes

for anyone with the same difficulties.

I was missing the Timezones in my startDateTime and endDateTime.

To add those i had to Format my DateTime i recive from my database.

This worked for me:

TimeAndOffset = new DateTimeOffset(reservation.beginnUhrzeit,
   TimeZoneInfo.Local.GetUtcOffset(reservation.beginnUhrzeit));
teamsCreate.startDateTime = TimeAndOffset.ToString("o");

reservation.beginnUhrzeit is my Database variable

teamsCreate is an Object i can later serialize to Json

After that everything worked.