How to build a Request Body when the MS Docs fail you Azure REST API

Or when you fail yourself… | Intro

I was creating schedules in Azure Automation and creating job schedules with parameters via the Azure REST API.

Strangely enough, it looked like the JSON from the example was wrong.

But unfortunately it turned out to be less true and I made a mistake myself.

However, it has often happened to me that the Request Body example does not match the actual JSON that is needed.
Below I’ve documented my research and ‘solution’ that you can use too.


Find the correct JSON Request Body with the Azure REST API call

In the intro I already use the Job Schedules for Azure Automation.
I use this Azure Rest API call as an example.

Information about Job Schedules can be found here:

The below was my original JSON. Do you see the mistake I made?

{
  "properties": {
    "schedule": {
      "name": "ScheduleNameGoesHere332204b5-debe-4348-a5c7-6357457189f2"
    },
    "runbook": {
      "name": "TestRunbook"
    }
  },
    "parameters": {
      "jobscheduletag01": "jobschedulevalue01",
      "jobscheduletag02": "jobschedulevalue02"
    }
}

Did you know that the Azure portal uses the same Azure Rest API?

And that you can see the calls to the Azure REST API in your browser?

By opening the Developer Tools (F12) you can see under the Network tab which requests are made when you manually create a Scheduled Job.
I’m using Microsoft Edge, but in general it’s the same on all browsers (as far as I know)

Open the Azure portal and navigate to the Runbook for the respective Scheduled Job you want to create.

  • Open the Developer Tools by pressing F12.
  1. Open the Network tab.
  2. And start adding a Schedule with parameters.
  3. As you can see, requests are placed under the Network tab that the Azure portal does.

The last one after creating the scheduled job is the one you must have.

How to build a Request Body when the MS Docs fail you Azure REST API
How to build a Request Body when the MS Docs fail you Azure REST API

When you open the request you can see whether this is a Get or Put method.
We need the Put method.

Open the Payload tab and then press View Source.
You now see the JSON that the Azure portal sends as Body with the Request.

You can now copy and paste this into an editor (this is purely for clarity).

Find the correct JSON Request Body with the API call
Find the correct JSON Request Body with the API call

I copy it to Visual Studio Code, save it as a .json file & Format the Document.

The full request looks like the below.

{
  "requests": [
    {
      "content": {
        "properties": {
          "parameters": {
            "test2": "\"Test 2\"",
            "test1": "\"Test 1\""
          },
          "runbook": {
            "name": "Test-BWRunbook"
          },
          "schedule": {
            "name": "Test-BWRunbook"
          },
          "runOn": ""
        }
      },
      "httpMethod": "PUT",
      "name": "37f5cbfb-c4ca-48c4-9997-5439c0f0a648",
      "requestHeaderDetails": {
        "commandName": "Microsoft_Azure_Automation."
      },
      "url": "URL"
    }
  ]
}

From the example we know that we only need the properties part.
It looks like the following in the Azure REST API.

{
  "properties": {
    "schedule": {
      "name": "ScheduleNameGoesHere332204b5-debe-4348-a5c7-6357457189f2"
    },
    "runbook": {
      "name": "TestRunbook"
    },
    "parameters": {
      "jobscheduletag01": "jobschedulevalue01",
      "jobscheduletag02": "jobschedulevalue02"
    }
  }
}

I don’t see anything wrong with my Request Body, what now?

I want to mention right away that JSON lines don’t have to be on specific lines, so you can build the properties in different orders.

That is an advantage when things go well right away, but it can also be a disadvantage.
For example, the JSON from the Azure Portal can contain more properties than is needed in your body.
It can also be in other orders, which makes comparison very difficult.

The following can be helpful.

Open Visual Studio Code.
Copy your JSON Request Body to a new file.
And copy the Azure Portal JSON Request Body to a new file as well.

As you can see I have both in Visual Studio Code with the respective names.

  • MyJson.json
  • AzurePortalJson.json
Find the correct JSON Request Body with the API call
Find the correct JSON Request Body with the API call

I right click my Request Body (MyJson.json)
And click on Select for Compare.

Find the correct JSON Request Body with the Azure REST API call
Find the correct JSON Request Body with the Azure REST API call

As you can probably guess, you now do this with the Azure Portal Request Body and then you choose Compare with Selected.

How to build a Request Body when the MS Docs fail you Azure REST API
How to build a Request Body when the MS Docs fail you Azure REST API

A comparison screen is now shown, in which it is indicated in red what differs from each other.
Because of this I immediately see that I have my parameters outside the properties brackets.

Find the correct JSON Request Body with the API call
Find the correct JSON Request Body with the API call

If there are a lot of red lines, then I would see if you can adjust it per line to the Azure Portal JSON to be able to compare as well.

Published by

Bas Wijdenes

My name is Bas Wijdenes and I work as a PowerShell DevOps Engineer. In my spare time I write about interesting stuff that I encounter during my work.

Leave a Reply

Your email address will not be published. Required fields are marked *