HTTP Hello World - Get
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
输出“Hello world!”以响应 GET 请求的函数。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
C#
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Go
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Java
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
PHP
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Ruby
如需向 Cloud Run functions 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis webpage showcases code samples for creating a simple "Hello World!" function that responds to GET requests in various languages.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples demonstrate how to implement this function in C#, Go, Java, Kotlin, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample includes instructions to set up Application Default Credentials for authenticating to Cloud Run functions.\u003c/p\u003e\n"],["\u003cp\u003eFurther resources are linked for users to explore detailed documentation, related language concepts, and deployment guides for creating functions.\u003c/p\u003e\n"],["\u003cp\u003eThe Google Cloud sample browser is linked for searching and filtering other code samples.\u003c/p\u003e\n"]]],[],null,["# HTTP Hello World - Get\n\nFunction that prints \"Hello world!\" in response to a GET request.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Create and deploy an HTTP Cloud Run function by using Java (1st gen)](/functions/1stgendocs/create-deploy-http-java-1st-gen)\n- [HTTP Tutorial](/functions/1stgendocs/tutorials/http-1st-gen)\n- [JVM Languages](/functions/1stgendocs/concepts/jvm-langs)\n- [Quickstart: Deploy a Cloud Run function\n using the gcloud CLI](/run/docs/quickstarts/functions/deploy-functions-gcloud)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using Google.Cloud.Functions.Framework;\n using Microsoft.AspNetCore.Http;\n using System.Threading.Tasks;\n\n namespace HelloWorld;\n\n public class Function : IHttpFunction\n {\n public async Task HandleAsync(HttpContext context)\n {\n await context.Response.WriteAsync(\"Hello World!\", context.RequestAborted);\n }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package helloworld provides a set of Cloud Functions samples.\n package helloworld\n\n import (\n \t\"fmt\"\n \t\"net/http\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.HTTP(\"HelloGet\", helloGet)\n }\n\n // helloGet is an HTTP Cloud Function.\n func helloGet(w http.ResponseWriter, r *http.Request) {\n \tfmt.Fprint(w, \"Hello, World!\")\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package functions;\n\n import com.google.cloud.functions.HttpFunction;\n import com.google.cloud.functions.HttpRequest;\n import com.google.cloud.functions.HttpResponse;\n import java.io.BufferedWriter;\n import java.io.IOException;\n\n public class HelloWorld implements HttpFunction {\n // Simple function to return \"Hello World\"\n @Override\n public void service(HttpRequest request, HttpResponse response)\n throws IOException {\n BufferedWriter writer = response.getWriter();\n writer.write(\"Hello World!\");\n }\n }\n\n### Kotlin\n\n import com.google.cloud.functions.HttpFunction\n import com.google.cloud.functions.HttpRequest\n import com.google.cloud.functions.HttpResponse\n import java.io.IOException\n import java.util.logging.Logger\n\n class HelloWorld : HttpFunction {\n // Simple function to return \"Hello World\"\n @Throws(IOException::class)\n override fun service(request: HttpRequest, response: HttpResponse) {\n response.writer.write(\"Hello World!\")\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const functions = require('@google-cloud/functions-framework');\n\n // Register an HTTP function with the Functions Framework that will be executed\n // when you make an HTTP request to the deployed function's endpoint.\n functions.http('helloGET', (req, res) =\u003e {\n res.send('Hello World!');\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Psr\\Http\\Message\\ServerRequestInterface;\n\n function helloGet(ServerRequestInterface $request): string\n {\n return 'Hello, World!' . PHP_EOL;\n }\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import functions_framework\n\n @functions_framework.http\n def hello_get(request):\n \"\"\"HTTP Cloud Function.\n Args:\n request (flask.Request): The request object.\n \u003chttps://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data\u003e\n Returns:\n The response text, or any set of values that can be turned into a\n Response object using `make_response`\n \u003chttps://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response\u003e.\n Note:\n For more information on how Flask integrates with Cloud\n Functions, see the `Writing HTTP functions` page.\n \u003chttps://cloud.google.com/functions/docs/writing/http#http_frameworks\u003e\n \"\"\"\n return \"Hello World!\"\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n FunctionsFramework.http \"hello_get\" do |_request|\n # The request parameter is a Rack::Request object.\n # See https://www.rubydoc.info/gems/rack/Rack/Request\n\n # Return the response body as a string.\n # You can also return a Rack::Response object, a Rack response array, or\n # a hash which will be JSON-encoded into a response.\n \"Hello World!\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]