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 Run 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_REQUEST args: url: URL_VALUE method: REQUEST_METHOD private_service_name: "REGISTERED_SERVICE" headers: HEADER_KEY:HEADER_VALUE ... body: BODY_KEY:BODY_VALUE ... query: QUERY_KEY:QUERY_VALUE ... auth: type: AUTH_TYPE scope: AUTH_SCOPE scopes: AUTH_SCOPE audience: AUDIENCE timeout: TIMEOUT_IN_SECONDS result: RESULT_VALUE
JSON
[ { "STEP_NAME": { "call": "HTTP_REQUEST", "args": { "url": "URL_VALUE", "method": "REQUEST_METHOD", "private_service_name": "REGISTERED_SERVICE", "headers": {"HEADER_KEY":"HEADER_VALUE", ... }, "body": {"BODY_KEY":"BODY_VALUE", ... }, "query": {"QUERY_KEY":"QUERY_VALUE", ... }, "auth": { "type":"AUTH_TYPE", "scope":"AUTH_SCOPE", "scopes":"AUTH_SCOPE", "audience":"AUDIENCE" }, "timeout": "TIMEOUT_IN_SECONDS" }, "result": "RESULT_VALUE" } } ]
Replace the following:
HTTP_REQUEST
: required. Use one of the following for HTTP requests:http.delete
http.get
http.patch
http.post
http.put
http.request
URL_VALUE
: required. URL where the request is sent.REQUEST_METHOD
: required if using call typehttp.request
. The type of HTTP request method to use. For example:GET
POST
PATCH
DELETE
REGISTERED_SERVICE
: optional. A registered Service Directory service name in the formatprojects/PROJECT_ID/locations/LOCATION/namespaces/NAMESPACE_NAME/services/SERVICE_NAME
. For more information, see Invoke a VPC Service Controls-compliant private endpoint.HEADER_KEY
:HEADER_VALUE
: optional. Header fields 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
orapplication/type+json
—must be a mapapplication/x-www-form-urlencoded
—must be an unencoded stringtext/type
—must be a string
If a
Content-Type
header is specified, the body is encoded as prescribed. For example, it might be JSON or URL-encoded.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 valueFor 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_KEY
:BODY_VALUE
: optional. Body fields to supply input to the API.If a
Content-Type
header is not specified, and if a request body is present, the following applies:- If the body value is bytes, the header is set to
Content-Type: application/octet-stream
. - Otherwise, the body is JSON-encoded and the header is set to
Content-Type: application/json; charset=utf-8
.
YAML
body: requests: - image: source: gcsImageUri: ${gsUri} features: - type: LABEL_DETECTION - type: SAFE_SEARCH_DETECTION - type: IMAGE_PROPERTIES result: imageAnalysisResponse
JSON
{ "requests":[ { "image": { "source": { "gcsUri": "img.png" } }, "features": [ { "type":"LABEL_DETECTION" }, { "type":"SAFE_SEARCH_DETECTION" }, { "type":"IMAGE_PROPERTIES" }, ] } ] }
- If the body value is bytes, the header is set to
QUERY_KEY
:QUERY_VALUE
: optional. Query fields to supply input to the API.AUTH_TYPE
: optional. Required if the API being called requires authentication. Use eitherOIDC
orOAuth2
. For more information, see Make authenticated requests from a workflow.AUTH_SCOPE
: optional. Limits an application's access to a user's account. Use either thescope
orscopes
key.The
scope
key supports either a string or list of strings. For example:"https://www.googleapis.com/auth/cloud-platform"
or
["https://www.googleapis.com/auth/cloud-platform", "scope2", "scope3"]
The
scopes
key, in addition to supporting a string or list of strings, supports space and comma-separated strings. For example:"https://www.googleapis.com/auth/cloud-platform scope2 scope3"
or
"https://www.googleapis.com/auth/cloud-platform,scope2,scope3"
For more information, see OAuth 2.0 Scopes for Google APIs.
AUDIENCE
: optional. Specifies the audience for the OIDC token. By default, it's set to the same value asurl
; however, it should be set to your service's root URL. For example:https://region-project.cloudfunctions.net/hello_world
.
TIMEOUT_IN_SECONDS
: optional. How long in seconds a request is allowed to run before throwing an exception. The maximum is 1800 seconds.RESULT_VALUE
: optional. Variable name where the result of an HTTP invocation step is stored.
Access HTTP response data saved in a variable
If the Content-Type
header for the response specifies an
application/json
media type, the JSON response that is stored in a
variable is automatically converted to a map that can be accessed.
If necessary, modify the API that is being called to specify an
application/json
media type for the Content-Type
response header. Otherwise, you can use the
json.decode
and text.encode
functions to
convert the response body into a map. For example:
json.decode(text.encode(RESPONSE_FROM_API))
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|headers.PATH_TO_FIELD}
Replace the following:
VARIABLE_NAME
: the name of the workflow variable where you saved a JSON response.body
: use thebody
field to access the body of the HTTP response.code
: use thecode
field to access the HTTP response code.headers
: use theheaders
field to access the HTTP response headers by name.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 ofobject1.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
Unless you input your own search term, this sample uses your Google Cloud location to construct a search term, which it passes to the Wikipedia API. A list of related Wikipedia articles is returned.
YAML
JSON
Make an external HTTP POST request
This sample makes a POST request to an external HTTP endpoint.
YAML
JSON
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
JSON
Use OIDC to authenticate when making a request to Cloud Run 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
JSON
Catch and handle HTTP request errors
This sample implements a custom exception handler based on the HTTP status code returned by the GET request. The workflow catches a potential exception and returns a predefined error message. If an exception is not recognized, the workflow execution fails and throws the exception as returned by the GET request. For other error tags, see Workflow errors.
YAML
JSON
What's next
- Use Workflows with Cloud Run and Cloud Run functions tutorial
- Invoke a VPC Service Controls-compliant private endpoint
- Invoke a private on‑prem, Compute Engine, GKE, or other endpoint by enabling IAP
- Workflows syntax reference