简体   繁体   English

AWS API 网关 HTTP API 如何传递字符串查询参数?

[英]AWS API Gateway HTTP API how to pass string query params?

So I am making an app and need AWS API Gateway.所以我正在制作一个应用程序并需要 AWS API 网关。 I want to use HTTP API instead of REST API.我想用 HTTP API 代替 REST ZDB974238714CA8DE634A7CE1D083A14 My code looks like this我的代码看起来像这样

package main

import (
    "database/sql"
    "fmt"
    "strings"

    "github.com/aws/aws-lambda-go/lambda"
    _ "github.com/lib/pq"
)

here I make a connection to the database


func fetch(inte string, input string) string {
    if err != nil {
        panic(err)
    }
    switch inte {
    case "00":
{
            res = append(res, response)
        }

        switch len(res) {
        case 0:
            return "401"
        }
        
    case "01":
        
        }

        switch len(res) {
        case 0:
            return "402"
        }
        
    }

    return "404"
}

type LambdaEvent struct {
    Req string `json:"req"`
    Num string `json:"num"`
}

type LambdaResponse struct {
    Res string `json:"res"`
}

func LambdaHandler(event LambdaEvent) (LambdaResponse, error) {
    res := fetch(event.Num, event.Req)
    return LambdaResponse{
        Res: res,
    }, nil
}

func main() {
    lambda.Start(LambdaHandler)
}

So as you see this is not the full code.如您所见,这不是完整的代码。 I make a connection to the database and and work with the requests string query.我与数据库建立连接并使用请求字符串查询。 so I tried the same with http api but it just gives me the 404 meaning the http api doesn't pass the query string to the lambda so how do I make my api pass the data to the lambda. so I tried the same with http api but it just gives me the 404 meaning the http api doesn't pass the query string to the lambda so how do I make my api pass the data to the lambda. Rest api works HTTP doesn't. Rest api 工作 HTTP 不工作。 Thanks for any help.谢谢你的帮助。

If you are deploying your lambdas and api-gateway with serverless framework you can do something like this:如果您使用无服务器框架部署 lambdas 和 api-gateway,您可以执行以下操作:

hello:
  handler: src/hello.handler
  name: hello
  events:
    - http:
        path: car/{id}/color/{color}
        method: get

I'm not familiar with Serverless Frameworks for APIGW, but manipulating QueryString parameters is built into the APIGW Console.我不熟悉 APIGW 的无服务器框架,但操作 QueryString 参数是内置在 APIGW 控制台中的。 Just login to AWS and search for APIGateway.只需登录 AWS 并搜索 APIGateway。 Edit your HTTP API and select Integrations from the menu on the left.从左侧的菜单中编辑 HTTP API 和 select Integrations Select the integration that maps to your Lambda function and Edit the Parameter Mappings on the right Select 映射到您的 Lambda function 的集成并编辑右侧的Parameter Mappings

APIGateway 的屏幕截图显示了如何覆盖查询字符串参数。

Assuming you are planning to use Lambda Proxy Integration in API Gateway , here are the changes that needs to be done to access the query parameters.假设您计划在 API 网关中使用 Lambda 代理集成,以下是访问查询参数需要进行的更改。

  • import github.com/aws/aws-lambda-go/events (This has all the relevant structs )进口github.com/aws/aws-lambda-go/events (这有所有相关的structs
  • Change the lambda handler to func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {将 lambda 处理程序更改为func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
  • Now you can access the query parameters as a Map at request.QueryStringParameters and execute your selection logic现在您可以在request.QueryStringParameters中以 Map 的形式访问查询参数并执行您的选择逻辑
  • When you return a response for API Gateway, ensure you follow the events.APIGatewayProxyResponse struct ie at least return a status code along with optional body, headers etc.当您返回 API 网关的响应时,请确保您遵循events.APIGatewayProxyResponse结构,即至少返回一个状态代码以及可选的正文、标题等。
  • No changes/config required at the API Gateway to pass the query parameters with Lambda proxy integration API 网关无需更改/配置即可通过 Lambda 代理集成传递查询参数

You can use your own structs for request and response, but they need to use the appropriate keys as defined in the events.APIGatewayProxyRequest and events.APIGatewayProxyResponse .您可以使用自己的structs进行请求和响应,但它们需要使用events.APIGatewayProxyRequestevents.APIGatewayProxyResponse中定义的适当键。

eg add the following in LambdaEvent struct to access the query string parameters.例如,在LambdaEvent结构中添加以下内容以访问查询字符串参数。

QueryStringParameters map[string]string `json:"queryStringParameters"`

If you are getting started with AWS Lambda, have a look at AWS SAM to keep things simple.如果您开始使用 AWS Lambda,请查看AWS SAM以简化操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将参数从 POST 从 Amazon API Gateway for Golang 传递到 AWS Lambda - Unable to pass a params from POST to AWS Lambda from Amazon API Gateway for Golang Lambda API网关POST参数 - Lambda API Gateway POST params API 网关集成请求 HTTP 标头未将查询字符串映射到标头 - API-Gateway Integration Request HTTP Header not mapping query string to header Go lang中的AWS API Gateway客户端证书 - AWS API Gateway client certificates in Go lang 如何将多个文件上传并解析到在 AWS Lambda 上运行的 API 网关后面的 GoLang API? - How to upload and parse multiple files to a GoLang API behind API Gateway running on AWS Lambda? Amazon API Gateway HTTP API: Custom types in lambda functions in go - Amazon API Gateway HTTP API: Custom types in lambda functions in go Golang AWS API Gateway无效字符'e'寻找值的开始 - Golang AWS API Gateway invalid character 'e' looking for beginning of value AWS API Gateway WebSockets [POST]@connections 返回 404 NotFound - AWS API Gateway WebSockets [POST]@connections Returning 404 NotFound 将 golang 服务器应用程序移植到 aws lambda + api 网关 - Porting a golang server app to aws lambda + api gateway 在 Go 中没有 API 网关的 AWS lambda 的函数 URL - AWS lambda's function URL without API Gateway in Go
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM