JSON to String API GATEWAY ( AWS )

Asked

Viewed 164 times

0

I need to receive a JSON in my API GATEWAY and pass to my LAMBDA ( GOLANG ) a STRING, The Structure Apigatewayproxyrequest the body is String type, when I try to call the lambda passing the json the same break, i need to create a model that converts the received JSON into the STRING gateway before going pro lambda.

Example of how it is today :

##set($allParams = $input.params())
{
## Repassando o json para o lambda
"body":"$util.escapeJavaScript($input.body)",
##"body":"$util.escapeJavaScript($input.body).replaceAll('\"','\\\"')",
## Populando o Headers
"headers": {
    ## Pegando todos os parametros do header e setando
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))"
    #if($foreach.hasNext),#end
    #end
}
}

Where BODY is of type STRING. When I pass something like :

"body":{"email":"[email protected]","senha":"1234"}

I get error , but if I pass :

"body":"{\"email\":\"[email protected]\",\"senha\":\"1234\"}"

Works normally, already removed the util.escape and nothing I need a model to solve this, taking the JSON and converting to the string type that works , so when I get to the lambda I parse.

Struct lambda

type APIGatewayProxyRequest struct {
Resource                        string                        `json:"resource"` // The resource path defined in API Gateway
Path                            string                        `json:"path"`     // The url path for the caller
HTTPMethod                      string                        `json:"httpMethod"`
Headers                         map[string]string             `json:"headers"`
MultiValueHeaders               map[string][]string           `json:"multiValueHeaders"`
QueryStringParameters           map[string]string             `json:"queryStringParameters"`
MultiValueQueryStringParameters map[string][]string           `json:"multiValueQueryStringParameters"`
PathParameters                  map[string]string             `json:"pathParameters"`
StageVariables                  map[string]string             `json:"stageVariables"`
RequestContext                  APIGatewayProxyRequestContext `json:"requestContext"`
Body                            string                        `json:"body"`
IsBase64Encoded                 bool                          `json:"isBase64Encoded,omitempty"`
}
  • good morning, you could insert error?

  • What if you parse json for string within the gateway api’s capacity? In the resource configuration there is the option to use "Content Handling" to force the conversion to text. This combined with the Mapping Templates feature can convert json to a template that is easy to fulfill the application’s contract. Just a hint focusing more on the aws gateway api than modifying the application itself.

1 answer

0

The Amazon Gateway API only receives if it is a string, if you pass any other type of data parse error happens, and also Response has a defined structure. In case you can transform your struct into []byte and then pass as string, example as I do:

func buildBody(statusCode int, body []byte) events.APIGatewayProxyResponse {
    return events.APIGatewayProxyResponse{
        StatusCode: statusCode,
        Headers: map[string]string{
            "Content-Type":                 "application/json; charset=utf-8",
            "Access-Control-Allow-Origin":  "*",
            "Access-Control-Allow-Methods": "OPTIONS, HEAD, GET, POST, PUT, DELETE",
            "Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with",
            "X-Requested-With":             "*",
        },
        MultiValueHeaders: nil,
        Body:              string(body),
        IsBase64Encoded:   false,
    }
}

Documentation about what I’m talking about: https://aws.amazon.com/pt/premiumsupport/knowledge-center/malformed-502-api-gateway/

The body field, if you’re returning JSON, must be converted to a string to Prevent further problems with the Response. You can use JSON.stringify to Handle this in Node.js functions. Other runtimes require Different Solutions, but the Concept is the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.