API Management Gotcha – Empty Body Definition

A while ago I saw a question in the forums where someone was trying to use a POST operation using the API Management action in Logic Apps and the action card didn’t have any way to input the body object.

To replicate the issue I’ve create a very simple logic app, so simple in fact that I didn’t even bother to define a trigger schema. After creating a new API using that logic app, to my surprise I also couldn’t find any input for the message body.After scratching my head a bit and sending a couple of emails, Jon Fancey pointed me out to the right direction by asking if the API was created from a logic app that didn’t have a trigger schema. So that was the culprit…

And indeed, looking at the Swagger generated by APIM, the definition for the body of the operation was set as empty.

swagger: '2.0'
info:
  title: LogicApp-US-Region
  version: '1.0'
  description: Azure Logic App.
host: xxxx.northcentralus.logic.azure.com
basePath: /logicapps/us
schemes:
  - https
securityDefinitions:
  apiKeyHeader:
    type: apiKey
    name: Ocp-Apim-Subscription-Key
    in: header
  apiKeyQuery:
    type: apiKey
    name: subscription-key
    in: query
security:
  - apiKeyHeader: []
  - apiKeyQuery: []
paths:
  /:
    post:
      description: Trigger a run of the logic app.
      operationId: manual-invoke
      parameters:
        - name: Content-Type
          in: header
          description: ''
          required: true
          type: string
          default: application/json
          enum:
            - application/json
        - name: request-manual
          in: body
          schema:
            $ref: '#/definitions/request-manual'
          description: The request body.
      consumes:
        - application/json
      responses:
        '200':
          description: The Logic App Response.
          schema:
            $ref: '#/definitions/ManualPathsInvokePost200ApplicationJsonResponse'
      produces:
        - application/json
definitions:
  request-manual:{}
  ManualPathsInvokePost200ApplicationJsonResponse:
    type: object

Luckily, the fix for that was as simple as manually updating the swagger definition of the operation (which is made simple in the API Management blade within the Azure Portal). To access the definition, I had to connect to my API, select all operations, and edit the front end.

changing the Azure Swagger definition.That took me to the OpenAPI specification area, where I could manually update the API definition:

OpenAPI ScreenScrolling down to the definition of request-manual, I just needed to change it to:

definitions:
  request-manual:
    type: object
  ManualPathsInvokePost200ApplicationJsonResponse:
    type: object

This gave me the required result. Now the action card renders as expected:

Manual invoke with input area for body part.Playing with the definition a bit more, I found that “strong-typing” request-manual with some well defined properties, is reflected in the action card. For example, this definition:

request-manual:
    type: object
    required:
      - message
      - code
    properties:
      message:
        type: string
      code:
        type: integer

Generates this action card:

That was fun and all, but it would be avoided if I only had bothered to define the schema of the manual trigger. On the other hand, I think that API Management could be user friendly enough to include the solo “object” for definitions for empty objects…

#notetoself: To avoid this trouble, I just need to make sure I define the schema of a manual trigger when I create a new logic app. I found that if I provide an empty json – like {} – to the sample message, a new schema of type object will be created, so at minimum I can do that.

BTW, making sure that you have a schema defined, will also help you when trying to call a logic app from another.

I hope you enjoyed. See you next time.


Posted

in

, ,

by

Comments

Leave a Reply

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