First off, one might say this question is very similar to HTTP request body not getting to AWS lambda function via AWS API Gateway or Getting json body in aws Lambda via API gateway
However, none of these questions address using Golang, and the problem I have been having is finding an equivalent of the event
parameter used everywhere in the Node.js documentation.
Here's my Lambda Function:
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"log"
)
type MyReturn struct {
Response string `json:"response"`
}
type APIGWResponse struct {
IsBase64Encoded bool `json:"isBase64Encoded"`
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
func handle(ctx context.Context, name MyReturn) (APIGWResponse, error) {
log.Print("Called by ", name)
log.Print("context ", ctx)
headers := map[string]string{"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"}
code := 200
response, error := json.Marshal(myReturn{Response:"Hello, " + name.Body})
if error != nil {
log.Println(error)
response = []byte("Internal Server Error")
code = 500
}
return APIGWResponse{true, code, headers, string(response)}, nil
}
func main() {
lambda.Start(handle)
}
Problem: the MyReturn
object is not being filled with any value when called from API GW. The line log.Print("Called by ", name)
results in nothing being appended to the Called by
string.
Request to API GW:
POST -> body: '{"name":"Bob"}', headers: {'Content-Type': 'application/json'}
This is being performed in pure JS as follows:
const BASE_URL = "https://my_api_id.execute-api.us-east-1.amazonaws.com/prod/";
const TRIGGER_URL = "my_lambda_function";
function toGW() {
fetch(BASE_URL + TRIGGER_URL, {
method: 'POST',
body: '{"name":"Bimesh"}',
headers:{
'Content-Type': 'application/json'
}
})
.then(data => data.json())
.then(json => console.log(json))
.catch(error => console.log(error));
}
And yet, the exact same body works when testing it from the AWS Lambda console.
Body:
{"name":"Bob"}
events.APIGatewayProxyRequest
as an input means writing pure json for the AWS Console "Test" functionality doesn't work anymore - I guess I need to wrap the request somehow, have you found a way around that? – Subdelirium