Multiple GRAPH API requests in HTTP Call

This is a knowledgebase item. Hope it helps you out someday if you want to get or post multiple GRAPH API requests in one HTTP call.

Issue

For a project, I am working on I had to create about 30 groups. I do not want to create these groups manually. So, I decided to automate the creation of the AAD groups. I am not a PowerShell guru, so I wanted to use the GRAPH API.

More information on how to create groups via the GRAPH API can be found here.

I started first by creating one group via the GRAPH API.

URL:

POST: https://graph.microsoft.com/v1.0/groups

Request body:

{
    "displayName": "Group-1",
    "groupTypes": [],
    "mailEnabled": false,
    "mailNickname": "Group-1",
    "securityEnabled": true
}

This was successful, I deleted the group, and my next step was to create multiple groups via the GRAPH API. So, I tried with two JSONs.

URL:

POST: https://graph.microsoft.com/v1.0/groups

Request body:

{
        "displayName": "Group-1",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Group-1",
        "securityEnabled": true
    },
    {
        "displayName": "Group-2",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Group-2",
        "securityEnabled": true
    }

In the request body my comma has a red line, let me check what the problem is.

I ignored the problem and posted my GRAPH API request. And as expected get an error:

So, I tried without the comma.

Request body:

{
        "displayName": "Ring-1",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Ring-1",
        "securityEnabled": true
    }
    {
        "displayName": "Ring-2",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Ring-2",
        "securityEnabled": true
    }

Now I get the same problem in my request body, but the problem is now on the accolade

I ignored the problem again and posted my GRAPH API request. As expected, I received the following error:

This will not be working. So, my next move was to try an array

Request body:

[ 
    {
        "displayName": "Group-1",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Group-1",
        "securityEnabled": true
    },
    {
        "displayName": "Group-2",
        "groupTypes": [],
        "mailEnabled": false,
        "mailNickname": "Group-2",
        "securityEnabled": true
    }
]

This time no problem with my request body

So, I submitted my post-action, and I received the Empty Payload. JSON content expected error.

The Solution to post multiple GRAPH API requests in one HTTP Call

I have found out that I had to use JSON batching. With JSON batching you can combine queries into one JSON object, this will automate your work, minimize your GRAPH API requests and it can improve the performance of your application.

More information about JSON batching can be found here.

But how does JSON batching actually work? You create a JSON with a requests array and in every request, you define the ID, method, URL, headers, and body.

URL:

POST: https://graph.microsoft.com/v1.0/$batch

Request body:

{
    "requests": [
        {
            "id": "1",
            "method": "POST",
            "url": "/{GRAPH API URL}/",
            "body": {your JSON},
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "id": "2",
            "method": "POST",
            "url": "/{GRAPH API URL/",
            "body": {your JSON},
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}

Example how to create multiple groups in one request

URL:

POST: https://graph.microsoft.com/v1.0/$batch

Request body:

{
    "requests": [
        {
            "id": "1",
            "method": "POST",
            "url": "/groups/",
            "body":
            {
                "displayName": "Group-1",
                "groupTypes": [],
                "mailEnabled": false,
                "mailNickname": "Group-1",
                "securityEnabled": true
            },
            "headers": {
                "Content-Type": "application/json"
            }
        },
        {
            "id": "2",
            "method": "POST",
            "url": "/groups/",
            "body":
            {
                "displayName": "Group-2",
                "groupTypes": [],
                "mailEnabled": false,
                "mailNickname": "Group-2",
                "securityEnabled": true
            },
            "headers": {
                "Content-Type": "application/json"
            }
        }
    ]
}

Results:

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.