AWS API Gateway Method请求路径参数不起作用

dmn0972

我正在尝试配置API网关以将请求路由到我的后端GO微服务上的特定路由。我正在使用GETVPC_LINK集成请求方法NLB,该方法路由到在中运行的后端微服务Fargate这是一个REST用编写的简单api GO在服务方面,在处理程序中,我对阶段路线做出了全部捕获响应,然后对/nmapscan路线做出了“ Hello World”响应,这就是我要达到的目标。但是,当我尝试使用调用URL来访问后端服务时,尽管请求路径在我的请求响应输出中显示为正确,但我仍会获得全部响应。我是API Gateway的新手,我感觉自己缺少一些简单的东西。另外,当我在本地运行容器时,我连接了一个外壳并运行了curl localhost:8000/v1/nmapscan并获得正确的“ Hello World!” 响应。以下是响应,我的配置以及流日志的响应:

RESPONSE:

user$ curl -v -X GET https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/v1/nmapscan/
> GET /v1/nmapscan/ HTTP/2
> Host: xxxxxxxxxx.execute-api.us-east-1.amazonaws.com
> User-Agent: curl/7.54.0
> Accept: */*
> 
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200 
< content-type: application/json
< content-length: 27
< date: Wed, 25 Sep 2019 20:24:16 GMT
< x-amzn-requestid: df90f051-dbdd-405f-a708-73668ad955f1
< x-amz-apigw-id: XXXXXXXXXXX=
< x-amzn-trace-id: Root=1-5d8bccf0-44ebf9c5af13c90e1636de42
< x-cache: Miss from cloudfront
< via: 1.1 b7ddb18a56b4bad68ca78b085e9ca451.cloudfront.net (CloudFront)
< x-amz-cf-pop: EWR52-C2
< x-amz-cf-id: lvT1CGlv2fboFJ5AxE917Jr61Nwb4fQOwbranZ3s_vz0EJULhcwudQ==
< 
* Connection #0 to host xxxxxxxxx.execute-api.us-east-1.amazonaws.com left intact
This is the catch all path!

如您所见,这将返回catch all响应。它应该返回“ Hello World!”。

Configuration:

resource "aws_api_gateway_rest_api" "GOAPI" {
  name        = "GO"
  description = "REST API for GO APIs"
}

resource "aws_api_gateway_resource" "test" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  parent_id   = "${aws_api_gateway_rest_api.GOAPI.root_resource_id}"
  path_part   = "nmapscan"
}

resource "aws_api_gateway_method" "testmethod" {
  rest_api_id   = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id   = "${aws_api_gateway_resource.test.id}"
  http_method   = "GET"
  authorization = "NONE"
  request_parameters = {
    "method.request.path.nmapscan" = true
  }
}

resource "aws_api_gateway_integration" "integrationtest" {
  connection_type = "VPC_LINK"
  connection_id   = "${aws_api_gateway_vpc_link.test.id}"
  type = "HTTP"
  integration_http_method = "GET"
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"
  uri = "${format("http://%s:8000", aws_lb.myapis.dns_name)}"

//  request_parameters = {
//    "integration.request.path.nmapscan" = "method.request.path.nmapscan"
//  }
}


resource "aws_api_gateway_method_response" "test-200" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"
  status_code = "200"

  response_models = {
    "application/json" = "Empty"
  }
}

resource "aws_api_gateway_integration_response" "testintegrationresponse" {
  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  resource_id = "${aws_api_gateway_resource.test.id}"
  http_method = "${aws_api_gateway_method.testmethod.http_method}"

  status_code = "${aws_api_gateway_method_response.test-200.status_code}"

  response_templates = {
    "application/json" = ""
  }
}

resource "aws_api_gateway_deployment" "testdeploy" {
  depends_on = ["aws_api_gateway_integration.integrationtest"]

  rest_api_id = "${aws_api_gateway_rest_api.GOAPI.id}"
  stage_name = "v1"
}

在此处输入图片说明

在此处输入图片说明

Flow Logs:

(fdaa14d3-08df-4847-ba63-a9644a65d265) Method request body before transformations:
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request URI: http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request headers: {x-amzn-apigateway-api-id=cqq6k2xrw3, Accept=application/json, User-Agent=AmazonAPIGateway_cqq6k2xrw3, Host=xxx-yyy-zzzzzz.elb.us-east-1.amazonaws.com, X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint request body after transformations:
(fdaa14d3-08df-4847-ba63-a9644a65d265) Sending request to http://xxx-yyy-zzzzzzz.elb.us-east-1.amazonaws.com:8000
(fdaa14d3-08df-4847-ba63-a9644a65d265) Received response. Status: 200, Integration latency: 15 ms
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint response headers: {Content-Type=application/json, Date=Thu, 26 Sep 2019 03:22:22 GMT, Content-Length=27}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Endpoint response body before transformations: This is the catch all path!
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method response body after transformations: This is the catch all path!
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method response headers: {X-Amzn-Trace-Id=Root=1-5d8c2eee-51c73680b040aea02ab1dd14, Content-Type=application/json}
(fdaa14d3-08df-4847-ba63-a9644a65d265) Successfully completed execution
(fdaa14d3-08df-4847-ba63-a9644a65d265) Method completed with status: 200
无路径特别

您的帖子看起来像这样

如果您确信这不是该响应中提到的内容,那么我将检查您的集成请求中的路由。

您的curl请求的URI是/ v1 / nmapscan /,这意味着API网关将查看STAGE v1资源/ nmapscan。一旦发生这种情况,API GW就会根据集成请求中配置的uri将请求发送到VPC链接。我对Terraform不太熟悉,但是您似乎将其发送到:

http://aws_lb.myapis.dns_name:8000

I also see that you have "request parameters" defined in terraform(but maybe commented out?):

    //  request_parameters = {
    //    "integration.request.path.nmapscan" = "method.request.path.nmapscan"
    //  }

I would check that after deployment, the Integration Request in the API GW Console shows that this is being routed to the proper route. Without knowing for sure, I'm assuming your application path is actually http://aws_lb.myapis.dns_name/nmapscan:8000 and that's not being passed correctly to the NLB because it isn't properly mapping for some reason in the Integration Request.

综上所述,查看将请求发送到NLB时URI是什么的最简单方法是启用API网关执行日志,并在这些日志上启用完整的请求/响应数据。它会给你类似的东西:


    (32e76dc1-2e80-11e9-b849-1d4cbf2f1403)转换后的端点请求主体:{“ resource”:“ / testproxy”,“ path”:“ / testproxy”,“ httpMethod”:“ GET”,“ headers”:[TRUNCATED ] 
    (32e76dc1-2e80-11e9-b849-1d4cbf2f1403)正在向[TRUNCATED]发送请求

为了简洁起见,我将其截断,但您可以在其中看到路径定义。仔细研究一下以缩小错误的产生源将很有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AWS API Gateway:错误429请求太多

来自分类Dev

如何访问AWS API Gateway的path参数?

来自分类Dev

AWS API Gateway:传递所有参数

来自分类Dev

邮递员调用AWS API Gateway触发AWS Lambda函数不起作用

来自分类Dev

带有Python-EVE“位置”的AWS API Gateway在已部署的API中不起作用

来自分类Dev

带有动态URL路径参数的AWS API Gateway

来自分类Dev

AWS API Gateway部署的API无法解析请求正文

来自分类Dev

AWS API Gateway HTTP API参数与Terraform的映射

来自分类Dev

是否可以在AWS API Gateway中使用通配符或全部路径

来自分类Dev

AWS API Gateway:状态码413,请求实体太大

来自分类Dev

AWS API Gateway:限制来自单个IP的请求

来自分类Dev

AWS API Gateway覆盖集成请求映射模板

来自分类Dev

AWS API Gateway:状态码413,请求实体太大

来自分类Dev

到AWS API Gateway的Ajax请求无法解析json响应

来自分类Dev

每日数据加载的 AWS API Gateway POST 请求

来自分类Dev

从AWS API Gateway返回HTML

来自分类Dev

AWS Api Gateway与Dynamo的集成

来自分类Dev

阻止AWS API Gateway重新排序URL参数?

来自分类Dev

带有AWS WAF的AWS API Gateway

来自分类Dev

从AWS API Gateway导出API定义

来自分类Dev

使用AWS Api Gateway进行Api合成

来自分类Dev

消耗AWS API Gateway API的问题

来自分类Dev

AWS API Gateway-调用API方法时出错-'无法将请求正文解析为json'

来自分类Dev

如何为AWS API Gateway认证用户?

来自分类Dev

AWS API Gateway更改内容类型

来自分类Dev

使用API Gateway的AWS Lambda错误消息

来自分类Dev

捕获AWS Api Gateway中的超时错误

来自分类Dev

Haproxy授权来自AWS API Gateway的流量

来自分类Dev

AWS API Gateway + DynamoDB角色ARN错误

Related 相关文章

热门标签

归档