HTTP Triggers
You can invoke Cloud Functions with an HTTP request using thePOST
,
PUT
, GET
, DELETE
, and OPTIONS
HTTP methods. To create an HTTP endpoint
for your function, specify --trigger-http
as the trigger type when deploying
your function. From the caller's perspective, HTTP invocations are synchronous,
meaning that the result of the function execution will be returned in the
response to the HTTP request.
For example, the following gcloud
command deploys a function to the Node.js
runtime that will be triggered by HTTP requests:
gcloud functions deploy FUNCTION_NAME --runtime nodejs16 --trigger-http --allow-unauthenticated
HTTP functions require
authentication by default. The
--allow-unauthenticated
flag lets you reach the function
without authentication.
To require authentication, omit the flag.
You can use the commonly available cURL command-line utility to invoke HTTP
functions. The following cURL command makes a POST
request with some data to
the URL of a deployed HTTP function:
curl -X POST "https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME" -H "Content-Type:application/json" --data '{"name":"Keyboard Cat"}'
where
YOUR_REGION
is the region where your function is deployed,YOUR_PROJECT_ID
is your Google Cloud Platform project ID, andFUNCTION_NAME
is the name of the HTTP function you deployed.
The trigger URL for an HTTP function can be seen after the function is
deployed, or queried at any time using the gcloud functions describe
command.
You can also trigger your HTTP functions directly through the gcloud CLI.
The following command calls a function and sends it some data in the body
of an HTTP POST
request:
gcloud functions call FUNCTION_NAME --data '{"name":"Keyboard Cat"}'
Code sample
Node.js
Python
Go
Java
C#
Ruby
PHP
HTTP Request/Response Structure
HTTP-triggered functions receive events via language-idiomatic HTTP frameworks. See the code samples above for more information.
Next steps
See the HTTP Tutorial for an example of how to implement an HTTP function, or HTTP Functions to learn more about writing HTTP functions.