aws apigateway 使用restapi集成http
创始人
2024-05-29 06:52:07
0

参考资料

  • https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/services-apigateway-tutorial.html

restapi代理集成http

在 HTTP 代理集成中,apigateway会将客户端提交的方法请求传递至后端。传递的请求数据包括请求标头、查询字符串参数、URL 路径变量和payload

后端负责传入请求数据解析,确定要返回的响应。HTTP 代理集成使得客户端和后端可以直接交互,而不受 API Gateway 的任何干预。

因此,开发人员必须向客户端开发人员讲清楚资源和对应的操作

创建httpapi名称为ProxyResourceForPetStore

在这里插入图片描述

api的代理资源路径{pet+} 成为 endpoint url 下任何后端端点的占位符

官方为我们提供了一个测试的api

$ curl http://petstore-demo-endpoint.execute-api.com/petstore/pets
[{"id": 1,"type": "dog","price": 249.99},{"id": 2,"type": "cat","price": 124.99},{"id": 3,"type": "fish","price": 0.99}
]

将endpoint url设置为http://petstore-demo-endpoint.execute-api.com/{proxy}

在这里插入图片描述

这之后的配置就和apigateway无关了,还需要满足以下要求

  • 确保后端能够访问
  • 客户端输入正确
  • 后端能够正确处理请求,apigateway不协调前后端的交互

我们在控制台测试已经能够请求到数据了

在这里插入图片描述

在终端测试也是一样的,注意要开启method的iam认证

$ awscurl --service execute-api   --region cn-north-1  https://74jp7wfz0k.execute-api.cn-north-1.amazonaws.com.cn/test/petstore/pets
[{"id": 1,"type": "dog","price": 249.99},{"id": 2,"type": "cat","price": 124.99},{"id": 3,"type": "fish","price": 0.99
}

再试试其他的公开api,例如https://tenapi.cn/v2/{proxy}

$ curl https://tenapi.cn/v2/yiyan -X POST -d 'format=json'
{"code": 200,"msg": "success","data": {"id": "1318780019000","hitokoto": "败者死于绝望,胜者死于渴望。","cat": "a","catname": "Anime - 动画","author": "哀川润","source": "戏言系列","date": "1318780019"}
}

然后我们就有了一个产生一句话的api

$ awscurl --service execute-api  --region cn-north-1  https://74jp7wfz0k.execute-api.cn-north-1.amazonaws.com.cn/test/yiyan
没错,不管是谁都是如此。
$ awscurl --service execute-api  --region cn-north-1  https://74jp7wfz0k.execute-api.cn-north-1.amazonaws.com.cn/test/yiyan
我每次读完一本书的时候,也会有种从梦中醒来一般的哀伤感觉呢。
$ awscurl --service execute-api  --region cn-north-1  https://74jp7wfz0k.execute-api.cn-north-1.amazonaws.com.cn/test/yiyan
想打的话就握紧你的拳头,不想打的话就别挡路啊,别用这种半吊子的态度来践踏别人的决心啊!

这意味着我们只要有一台web服务器,就能够集成apigateway并获取其更多功能

restapi非代理集成http

再来看看非代理集成http

创建api的步骤一致,创建资源时取消勾选代理资源,启用CORS

在这里插入图片描述

创建方法,非代理集成下我们可选择集成项目也变多了,但是需要指定请求方法

  • 这里创建get方法,url为http://petstore-demo-endpoint.execute-api.com/petstore/pets

  • http method的方法,是集成请求的方法谓词,可以和请求方法不一致,取决于请求目的

    For the integration request’s HTTP method, you must choose one supported by the backend. For HTTP or Mock integration, it makes sense that the method request and the integration request use the same HTTP verb. For other integration types the method request will likely use an HTTP verb different from the integration request. For example, to call a Lambda function, the integration request must use POST to invoke the function, whereas the method request may use any HTTP verb depending on the logic of the Lambda function.

在这里插入图片描述

  • 开启iam校验

控制台测试请求成功,并部署

在这里插入图片描述

终端测试访问,结果一致

$ awscurl --service execute-api   --region cn-north-1  "https://74jp7wfz0k.execute-api.cn-north-1.amazonaws.com.cn/test/pet?type=dog&page=2"
[{"id": 4,"type": "dog","price": 999.99},{"id": 5,"type": "dog","price": 249.99},{"id": 6,"type": "dog","price": 49.97}
]

关于请求参数映射的内容,可以继续看看

https://docs.amazonaws.cn/en_us/apigateway/latest/developerguide/api-gateway-create-api-step-by-step.html

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...