Make an HTTP request

You can define a workflow step that makes an HTTP call and assign the response from the call to a variable. For example, you can invoke a Google Cloud service such as Cloud Functions or Cloud Run through an HTTP request.

Invoke an HTTP endpoint

This type of step allows you to make an HTTP request. Both HTTP and HTTPS requests are supported. The most common HTTP request methods have a call shortcut (such as http.get and http.post), but you can make any type of HTTP request by setting the call field to http.request and specifying the type of request using the method field.

YAML

  - STEP_NAME:
      call: {http.delete|http.get|http.patch|http.post|http.put|http.request}
      args:
          url: URL_VALUE
          [method: REQUEST_METHOD]
          [headers:
              KEY1:VALUE1
              ...]
          [body:
              KEY2:VALUE2
              ...]
          [query:
              KEY3:VALUE3
              ...]
          [auth:
              type:{OIDC|OAuth2}]
          [timeout: VALUE_IN_SECONDS]
      [result: RESPONSE_VALUE]
    

JSON

  [
    {
      "STEP_NAME": {
        "call": "{http.delete|http.get|http.patch|http.post|http.put|http.request}",
        "args": {
          "url": "URL_VALUE",
          ["method": "REQUEST_METHOD",]
          ["headers": {"KEY1":"VALUE1",
          ...
          },]
          ["body": {"KEY2":"VALUE2",
          ...
          },]
          ["query": {"KEY3":"VALUE3",
          ...
          },]
          ["auth": {"type":"{OIDC|OAuth2}"},]
          ["timeout": "VALUE_IN_SECONDS"]
        },
        ["result": "RESPONSE_VALUE"]
      }
    }
  ]
    
  • call—Required. Use one of the following for HTTP requests:
    • http.delete
    • http.get
    • http.patch
    • http.post
    • http.put
    • http.request
  • url—Required. URL where the request is sent.
  • method—Required if using call type http.request. The type of HTTP request method to use. For example:
    • GET
    • POST
    • PATCH
    • DELETE
  • headers—Optional. Field to supply input to the API.

    If using a Content-Type header to specify the media type of the request body, only the following types are supported:

    • application/json or application/type+json—must be a map
    • application/x-www-form-urlencoded—must be an unencoded string
    • text/type—must be a string

    If using a User-Agent header to identify the requesting user agent, the following applies:

    • The default is GoogleCloudWorkflows; (+https://cloud.google.com/workflows/docs)
    • If a value is specified, GoogleCloudWorkflows; (+https://cloud.google.com/workflows/docs) is appended to that value

      For example, if User-Agent: "MY_USER_AGENT_VALUE" is specified, the HTTP request header would be as follows (with a space between the specified value and the appended default):

      MY_USER_AGENT_VALUE GoogleCloudWorkflows; (+https://cloud.google.com/workflows/docs)
  • body—Optional. Field to supply input to the API.
  • query—Optional. Field to supply input to the API.
  • auth—Optional. Required if the API being called requires authentication. See Make authenticated requests for more information.
  • timeout—Optional. How long in seconds a request is allowed to run before throwing an exception. The maximum is 1800 seconds.
  • result—Optional. Variable name where the result of an HTTP invocation step is stored.

Access HTTP response data saved in a variable

When a response of type application/json is stored in a variable, the JSON response is converted to a map you access as a response body. Workflows includes a built-in parser for accessing this data. To access the fields from the HTTP response, use the following syntax:

${VARIABLE_NAME.body|code.PATH_TO_FIELD}
  • VARIABLE_NAME: the name of the workflow variable where you saved a JSON response.
  • body: use the body field to access the body of the HTTP response.
  • code: use the code field to access the HTTP response code.
  • PATH_TO_FIELD: the path to the field in the JSON response that you want to access. May be simply the name of the field, or if the field is nested inside an object, it may take the form of object1.object2.field.

For example, if an API returns {"age":50} and a workflow stores that response in a variable called age_response, the following example returns the value of the age field; in this case, 50:

age_response.body.age

Samples

These samples demonstrate the syntax.

Assign the response from an API call

This sample makes a call to a sample API. The returned day of the week is passed to the Wikipedia API. Relevant articles on Wikipedia about the current day of the week are returned.

YAML

- getCurrentTime:
    call: http.get
    args:
      url: https://us-central1-workflowsample.cloudfunctions.net/datetime
    result: currentTime
- readWikipedia:
    call: http.get
    args:
      url: https://en.wikipedia.org/w/api.php
      query:
        action: opensearch
        search: ${currentTime.body.dayOfTheWeek}
    result: wikiResult
- returnResult:
    return: ${wikiResult.body[1]}

JSON

[
  {
    "getCurrentTime": {
      "call": "http.get",
      "args": {
        "url": "https://us-central1-workflowsample.cloudfunctions.net/datetime"
      },
      "result": "currentTime"
    }
  },
  {
    "readWikipedia": {
      "call": "http.get",
      "args": {
        "url": "https://en.wikipedia.org/w/api.php",
        "query": {
          "action": "opensearch",
          "search": "${currentTime.body.dayOfTheWeek}"
        }
      },
      "result": "wikiResult"
    }
  },
  {
    "returnResult": {
      "return": "${wikiResult.body[1]}"
    }
  }
]

Make an external HTTP POST request

This sample makes a POST request to an external HTTP endpoint.

YAML

- get_message:
    call: http.post
    args:
      url: https://www.example.com/endpoint
      body:
        some_val: "Hello World"
        another_val: 123
    result: the_message
- return_value:
    return: ${the_message.body}

JSON

[
  {
    "get_message": {
      "call": "http.post",
      "args": {
        "url": "https://www.example.com/endpoint",
        "body": {
          "some_val": "Hello World",
          "another_val": 123
        }
      },
      "result": "the_message"
    }
  },
  {
    "return_value": {
      "return": "${the_message.body}"
    }
  }
]

Make an external HTTP GET request with headers

This sample makes an HTTP GET request with a custom header. You can also supply custom header definitions when making other types of HTTP requests.

YAML

- get_message:
    call: http.get
    args:
      url: https://www.example.com/endpoint
      headers:
        Content-Type: "text/plain"
      query:
        some_val: "Hello World"
        another_val: 123
    result: the_message
- return_value:
    return: ${the_message.body}

JSON

[
  {
    "get_message": {
      "call": "http.get",
      "args": {
        "url": "https://www.example.com/endpoint",
        "headers": {
          "Content-Type": "text/plain"
        },
        "query": {
          "some_val": "Hello World",
          "another_val": 123
        }
      },
      "result": "the_message"
    }
  },
  {
    "return_value": {
      "return": "${the_message.body}"
    }
  }
]

Use OIDC to authenticate when making a request to Cloud Functions

This sample makes an HTTP request using OIDC by adding an auth section to the args section of the workflow's definition, after specifying the URL.

YAML

- call_my_function:
    call: http.post
    args:
      url: https://us-central1-myproject123.cloudfunctions.net/myfunc1
      auth:
        type: OIDC
      body:
        some_val: "Hello World"
        another_val: 123
    result: the_message
- return_value:
    return: ${the_message.body}

JSON

[
  {
    "call_my_function": {
      "call": "http.post",
      "args": {
        "url": "https://us-central1-myproject123.cloudfunctions.net/myfunc1",
        "auth": {
          "type": "OIDC"
        },
        "body": {
          "some_val": "Hello World",
          "another_val": 123
        }
      },
      "result": "the_message"
    }
  },
  {
    "return_value": {
      "return": "${the_message.body}"
    }
  }
]

What's next