Using Microsoft Graph API HTTP Request/Response Documentation to Generate Code

This blog post describes how to use the raw HTTP request/response samples provided in the Microsoft Graph API documentation to generate Chilkat source code in your chosen programming language.

First, let’s have a look at one particular request. This is the “Create call” action found in the Teamwork and Communications –> Calls and online meetings –> Calls –> Call documentation at https://learn.microsoft.com/en-us/graph/api/application-post-calls?view=graph-rest-1.0&tabs=http

Go to the HTTP request at https://learn.microsoft.com/en-us/graph/api/application-post-calls?view=graph-rest-1.0&tabs=http#http-request

Look at the “HTTP” example in the “HTTP” tab. Here it is:

Note: The “Authorization: Bearer ” HTTP header field is missing from the raw HTTP request that is shown. You’ll need to add it in the next step.

Go to Chilkat’s online tool at https://tools.chilkat.io/httpRequestToCode

Copy the raw HTTP request into Chilkat’s online tool. Add the “Authorization: Bearer ” header field line directly under the “Content-Type” header. Choose your programming language, and generate.

For example, copying the following into Chilkat’s online tool:

POST https://graph.microsoft.com/v1.0/communications/calls
Content-Type: application/json
Authorization: Bearer 

{
  "@odata.type": "#microsoft.graph.call",
  "callbackUri": "https://bot.contoso.com/callback",
  "targets": [
    {
      "@odata.type": "#microsoft.graph.invitationParticipantInfo",
      "identity": {
        "@odata.type": "#microsoft.graph.identitySet",
        "user": {
          "@odata.type": "#microsoft.graph.identity",
          "displayName": "John",
          "id": "112f7296-5fa4-42ca-bae8-6a692b15d4b8"
        }
      }
    }
  ],
  "requestedModalities": [
    "audio"
  ],
  "callOptions": {
    "@odata.type": "#microsoft.graph.outgoingCallOptions",
    "isContentSharingNotificationEnabled": true
  },
  "mediaConfig": {
    "@odata.type": "#microsoft.graph.serviceHostedMediaConfig"
  }
}

If C# is the chosen programming language, the following is generated:

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

Chilkat.Http http = new Chilkat.Http();
bool success;

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// {
//   "@odata.type": "#microsoft.graph.call",
//   "callbackUri": "https://bot.contoso.com/callback",
//   "targets": [
//     {
//       "@odata.type": "#microsoft.graph.invitationParticipantInfo",
//       "identity": {
//         "@odata.type": "#microsoft.graph.identitySet",
//         "user": {
//           "@odata.type": "#microsoft.graph.identity",
//           "displayName": "John",
//           "id": "112f7296-5fa4-42ca-bae8-6a692b15d4b8"
//         }
//       }
//     }
//   ],
//   "requestedModalities": [
//     "audio"
//   ],
//   "callOptions": {
//     "@odata.type": "#microsoft.graph.outgoingCallOptions",
//     "isContentSharingNotificationEnabled": true
//   },
//   "mediaConfig": {
//     "@odata.type": "#microsoft.graph.serviceHostedMediaConfig"
//   }
// }

Chilkat.JsonObject json = new Chilkat.JsonObject();
json.UpdateString("\"@odata.type\"","#microsoft.graph.call");
json.UpdateString("callbackUri","https://bot.contoso.com/callback");
json.UpdateString("targets[0].\"@odata.type\"","#microsoft.graph.invitationParticipantInfo");
json.UpdateString("targets[0].identity.\"@odata.type\"","#microsoft.graph.identitySet");
json.UpdateString("targets[0].identity.user.\"@odata.type\"","#microsoft.graph.identity");
json.UpdateString("targets[0].identity.user.displayName","John");
json.UpdateString("targets[0].identity.user.id","112f7296-5fa4-42ca-bae8-6a692b15d4b8");
json.UpdateString("requestedModalities[0]","audio");
json.UpdateString("callOptions.\"@odata.type\"","#microsoft.graph.outgoingCallOptions");
json.UpdateBool("callOptions.isContentSharingNotificationEnabled",true);
json.UpdateString("mediaConfig.\"@odata.type\"","#microsoft.graph.serviceHostedMediaConfig");

http.SetRequestHeader("Content-Type","application/json");
// Adds the "Authorization: Bearer <access_token>" header.
http.AuthToken = "<access_token>";

Chilkat.HttpResponse resp = http.PostJson3("https://example.com/v1.0/communications/calls","application/json",json);
if (http.LastMethodSuccess == false) {
    Debug.WriteLine(http.LastErrorText);
    return;
}

Debug.WriteLine(Convert.ToString(resp.StatusCode));
Debug.WriteLine(resp.BodyStr);

You can also generate code to process the JSON response body by copying the sample JSON response body (excluding the response headers) into Chilkat’s online tool to generate JSON parsing code at https://tools.chilkat.io/jsonParse