Thomas Kiml
Thomas Kiml

Reputation: 13

AWS API Gateway HTTP API how to pass string query params?

So I am making an app and need AWS API Gateway. I want to use HTTP API instead of REST API. 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. Rest api works HTTP doesn't. Thanks for any help.

Upvotes: 0

Views: 4244

Answers (3)

GSSwain
GSSwain

Reputation: 6133

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.

  • import github.com/aws/aws-lambda-go/events (This has all the relevant structs)
  • Change the lambda handler to 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
  • When you return a response for API Gateway, ensure you follow the events.APIGatewayProxyResponse struct i.e. at least return a status code along with optional body, headers etc.
  • No changes/config required at the API Gateway to pass the query parameters with Lambda proxy integration

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.

e.g. add the following in LambdaEvent struct to access the query string parameters.

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

If you are getting started with AWS Lambda, have a look at AWS SAM to keep things simple.

Upvotes: 0

Chris Smith
Chris Smith

Reputation: 731

I'm not familiar with Serverless Frameworks for APIGW, but manipulating QueryString parameters is built into the APIGW Console. Just login to AWS and search for APIGateway. Edit your HTTP API and select Integrations from the menu on the left. Select the integration that maps to your Lambda function and Edit the Parameter Mappings on the right

Screenshot of APIGateway showing how to override query string parameters.

Upvotes: 1

Jordi Tanta Diaz
Jordi Tanta Diaz

Reputation: 36

If you are deploying your lambdas and api-gateway with serverless framework you can do something like this:

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

Upvotes: 0

Related Questions