[英]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
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 代理集成,以下是访问查询参数需要进行的更改。
github.com/aws/aws-lambda-go/events
(This has all the relevant structs
)github.com/aws/aws-lambda-go/events
(这有所有相关的structs
)func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
request.QueryStringParameters
and execute your selection logicrequest.QueryStringParameters
中以 Map 的形式访问查询参数并执行您的选择逻辑events.APIGatewayProxyResponse
struct ie at least return a status code along with optional body, headers etc.events.APIGatewayProxyResponse
结构,即至少返回一个状态代码以及可选的正文、标题等。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.APIGatewayProxyRequest
和events.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.