Search code examples
goaws-lambdahttprequestaws-api-gatewayaws-http-api

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.


Solution

  • 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