# JSON example
This example demonstrates writing a function that accepts and returns JSON.
The basic shape of the function handler looks like this:
```dart
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
}
```
The client will send a JSON document using the HTTP POST method. Here's an
example request:
```json
{
"name": "World"
}
```
The function will send a JSON document as the response. Here's an example
response after receiving the above request:
```json
{
"salutation": "Hello",
"name": "World"
}
```
The Functions Framework parses the request data to fit the shape of a
`GreetingRequest` object for you. Because a name might not have been sent with
the request, the function implementation provides a default name: `World`.
```dart
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
final name = request.name ?? 'World';
}
```
Finally, the function creates an object returns it. The Functions Framework will
take this and attempt "do the right" thing. In this case, the function is typed
to return a `FutureOr<GreetingResponse>`, which has a `toJson()` method, so the
framework will invoke this, then set the response body to the stringified result
and set the response header (`content-type`) to `application/json`).
```dart
@CloudFunction()
GreetingResponse function(Map<String, dynamic> request) {
final name = request['name'] as String ?? 'World';
final json = GreetingResponse(salutation: 'Hello', name: name);
return json;
}
```
The full code is shown below:
lib/functions.dart
```dart
import 'dart:async';
import 'package:functions_framework/functions_framework.dart';
import 'package:json_annotation/json_annotation.dart';
part 'functions.g.dart';
@JsonSerializable()
class GreetingRequest {
final String? name;
GreetingRequest({this.name});
// This class also includes two marshalling methods that are
// standard boilerplate that depend on generated code in 'functions.g.dart'
// To help with testing, this class also overrides the equality (`==`)
// operator, and therefore also `hashCode`.
}
@JsonSerializable()
class GreetingResponse {
final String salutation;
final String name;
GreetingResponse({required this.salutation, required this.name});
// This class also includes two marshalling methods that are
// standard boilerplate that depend on generated code in 'functions.g.dart'
// To help with testing, this class also overrides the equality (`==`)
// operator, and therefore also `hashCode`.
}
@CloudFunction()
GreetingResponse function(GreetingRequest request) {
final name = request.name ?? 'World';
final json = GreetingResponse(salutation: 'Hello', name: name);
return json;
}
```
## Generate project files
The Dart `build_runner` tool generates the following files
- `lib/functions.g.dart` - the part file for `GreetingResponse` serialization
- `bin/server.dart` - the main entry point for the function server app, which
invokes the function
Run the `build_runner` tool, as shown here:
```shell
$ dart run build_runner build
[INFO] Generating build script completed, took 337ms
[INFO] Reading cached asset graph completed, took 48ms
[INFO] Checking for updates since last build completed, took 426ms
[INFO] Running build completed, took 13ms
[INFO] Caching finalized dependency graph completed, took 29ms
[INFO] Succeeded after 51ms with 0 outputs (0 actions)
```
## Test the function
```shell
$ dart test
00:02 +1: All tests passed!
```
## Run the function
```shell
$ dart run bin/server.dart
Listening on :8080
```
From another terminal, send a JSON request:
```shell
$ curl -X POST -H "content-type: application/json" -d '{ "name": "World" }' -i localhost:8080
HTTP/1.1 200 OK
date: Sat, 19 Dec 2020 02:17:42 GMT
content-length: 37
x-frame-options: SAMEORIGIN
content-type: application/json
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
server: dart:io with Shelf
{"salutation":"Hello","name":"World"}
```
Tools like [curl] (and [postman]) are good for sending HTTP requests. The
options used in this example are:
- `-X POST` - send an HTTP POST request
- `-H "content-type: application/json"` - set an HTTP header to indicate that
the body is a JSON document
- `-d '{ "name": "World" }'` - set the request body to a JSON document
- `-i` - show the response headers (to confirm the response body content type is
also a JSON document)
The last line, separated by a blank line, prints the response body.
For more details on getting started or to see how to run the function locally on
Docker or deploy to Cloud Run, see these quick start guides:
- [Quickstart: Dart]
- [Quickstart: Docker]
- [Quickstart: Cloud Run]
<!-- reference links -->
[curl]: https://curl.se/docs/manual.html
[Quickstart: Dart]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/01-quickstart-dart.md
[Quickstart: Docker]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/02-quickstart-docker.md
[Quickstart: Cloud Run]: https://github.com/GoogleCloudPlatform/functions-framework-dart/blob/main/docs/quickstarts/03-quickstart-cloudrun.md
[postman]: https://www.postman.com/product/api-client/
没有合适的资源?快使用搜索试试~ 我知道了~
FaaS(函数即服务)框架,用于编写可移植的Dart函数___下载.zip
共307个文件
dart:130个
yaml:32个
md:28个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 79 浏览量
2023-04-18
00:38:54
上传
评论
收藏 479KB ZIP 举报
温馨提示
FaaS(函数即服务)框架,用于编写可移植的Dart函数___下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
FaaS(函数即服务)框架,用于编写可移植的Dart函数___下载.zip (307个子文件)
AUTHORS 334B
bashcmds 1KB
my_application.cc 4KB
main.cc 180B
generated_plugin_registrant.cc 164B
generated_plugin_registrant.cc 161B
generated_plugins.cmake 743B
generated_plugins.cmake 739B
win32_window.cpp 7KB
run_loop.cpp 2KB
flutter_window.cpp 2KB
utils.cpp 2KB
main.cpp 1KB
cloudevent.g.dart 21KB
json.g.dart 18KB
builder_test.dart 18KB
helloworld.g.dart 11KB
cloud_behavior_test.dart 10KB
cloud_event_test.dart 10KB
logging.dart 8KB
cli_test.dart 7KB
custom_type_test.dart 6KB
validate_templates.dart 6KB
generic_function_type.dart 6KB
builder.dart 5KB
json_targets.dart 5KB
function_test.dart 5KB
stagehand.dart 5KB
generate.dart 4KB
function_config.dart 4KB
functions.dart 4KB
function_test.dart 4KB
gcp_test.dart 4KB
gcp_project.dart 4KB
function_test.dart 4KB
valid_json_utils.dart 3KB
serve.dart 3KB
cloud_event_targets.dart 3KB
code_generator.dart 3KB
example.dart 3KB
test_utils.dart 3KB
greeting.dart 3KB
server.dart 3KB
common.dart 3KB
function_type_validator.dart 3KB
supported_function_type.dart 2KB
app.dart 2KB
test_utils.dart 2KB
typedefs.dart 2KB
version.dart 2KB
greet.dart 2KB
cloud_event.g.dart 2KB
log_severity.dart 2KB
json_request_utils.dart 2KB
serve.dart 2KB
common_test.dart 2KB
functions.dart 2KB
functions.dart 2KB
pub_upgrade.dart 2KB
directory.dart 2KB
home_page.dart 2KB
terminate.dart 2KB
function_test.dart 2KB
dartfn.dart 2KB
test_utils.dart 2KB
mock_test.dart 2KB
cloud_event.dart 2KB
valid_shelf_handlers.dart 2KB
server.dart 2KB
server.dart 2KB
server.dart 2KB
bad_request_exception.dart 2KB
command.dart 2KB
functions.dart 2KB
conformance_handlers.dart 2KB
logging_test.dart 2KB
config.dart 2KB
structured_mode_request.dart 1KB
binary_mode_request.dart 1KB
api.dart 1KB
pub_sub_types.dart 1KB
function_test.dart 1KB
request_context.dart 1KB
terminal.dart 1KB
run.dart 1KB
console.dart 1KB
function_target.dart 1KB
valid_custom_type_handlers.dart 1KB
app_model.dart 1KB
functions_framework.dart 1KB
function_test.dart 1KB
gcp.dart 1KB
json_handlers.dart 1KB
api_types.dart 1KB
api_types.dart 1KB
api_types.dart 1KB
http_utils.dart 1KB
function_test.dart 1KB
main.dart 1KB
printer.dart 1KB
共 307 条
- 1
- 2
- 3
- 4
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python 程序语言设计模式思路-创建型模式:原型模式:通过复制现有对象来创建新对象,面向对象编程
- 卸载软件geek卸载软件geek
- Python 程序语言设计模式思路-创建型模式:单例模式,确保一个类的唯一实例(装饰器)面向对象编程、继承
- skywalking-plugins.jar skywalking-alarm.jar
- 独栋别墅图纸D020-两层-10.00&11.00米- 施工图.dwg
- Python 程序语言设计模式思路-创建型模式:工厂模式,创建对象的统一接口,封装对象的创建逻辑
- python自学教程-05-json数据格式的介绍.ev4.rar
- python自学教程-04-自定义JavaScript.ev4.rar
- 《淘宝后台系统...》
- skywalking-plugins.jar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功