Invoke a POST API

This procedure shows how to create a flow to invoke a POST API and uses connection properties to update API host information.

Prerequisites:

The body is one of the action input schema properties and is passed to the target schema.

  1. Open the adapter definition document in Visual Studio Code.
  2. In the flows section of the document, define the flow with a single state of Operation type.

    Sample code:

    "flows": {  
       "postOrdersFlow" : {
          "id": "postOrdersFlow",
          "description": "postOrdersFlow",
          "version": "0.1",
          "start": "startState",
          "specVersion": "0.8",
          "functions": [
             
          ],
          "states": [
            {
              "actions": [
                
              ],
              "name": "startState",
              "type": "operation",
              "end": true
            }
          ]
        }
    }
  3. Define a function with a unique name, of type custom, and operation connectivity::rest.

    Sample code:

    "functions": [
            {
              "name": "postOrdersFunction",
              "operation": "connectivity::rest",
              "type": "custom"
            }
          ]
  4. Add an action to the state where:
    • The action refers to the function created in step 2.
    • Arguments to function define the HTTP method, URI, parameters (template and query), and body.
    • Response is passed through, and the result is set to output.

    Sample code:

    {
                  "functionRef": {
                    "refName": "postOrdersFunction",
                    "arguments": {
                      "uri": "${.connectionProperties.baseURL + \"/orders\"}",
                      "method": "POST",
                      "body": "${ .input.body }"
                    }
                  },
                  "actionDataFilter": {
                    "results": "${ { body: .body, headers: .headers } }",
                    "toStateData": "${ .output }"
                  }
                }
Complete sample code:
"flows": {   
   "postOrdersFlow" : {
      "id": "postOrdersFlow",
      "description": "postOrdersFlow",
      "version": "0.1",
      "start": "startState",
      "specVersion": "0.8",
      "functions": [
        {
          "name": "postOrdersFunction",
          "operation": "connectivity::rest",
          "type": "custom"
        }
      ],
      "states": [
        {
          "actions": [
            {
              "functionRef": {
                "refName": "postOrdersFunction",
                "arguments": {
                  "uri": "${.connectionProperties.baseURL + \"/orders\"}",
                  "method": "POST",
                  "body": "${ .input.body }"
                }
              },
              "actionDataFilter": {
                "results": "${ { body: .body, headers: .headers } }",
                "toStateData": "${ .output }"
              }
            }
          ],
          "name": "startState",
          "type": "operation",
          "end": true
        }
      ]
    }
}