aws apigateway 使用restapi集成http

参考资料

  • 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值