Using the sample code from the aws-lambda-go repo with sam local produces a response of 'Unexpected H'.
Code follows:
package main
import (
"errors"
"log"
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
var (
// ErrNameNotProvided is thrown when a name is not provided
ErrNameNotProvided = errors.New("no name was provided in the HTTP body")
)
// Handler is your Lambda function handler
// It uses Amazon API Gateway request/responses provided by the aws-lambda-go/events package,
// However you could use other event sources (S3, Kinesis etc), or JSON-decoded primitive types such as 'string'.
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
log.Printf("Body size = %d.\n", len(request.Body))
log.Println("Headers:")
for key, value := range request.Headers {
log.Printf(" %s: %s\n", key, value)
}
// If no name is provided in the HTTP request body, throw an error
if len(request.Body) < 1 {
return events.APIGatewayProxyResponse{}, ErrNameNotProvided
}
respString := "Hello "+ request.Body
log.Printf ("Returning string: %v", respString)
return events.APIGatewayProxyResponse{Body: respString, StatusCode: 200, IsBase64Encoded: false}, nil
}
func main() {
lambda.Start(handleRequest)
}
I don't use golang myself, but I've seen this kind of message in Postman. If you are using Postman, try switching the way you look at the response body from Pretty to Raw and see if it makes a difference.
Can you try with latest SAM CLI v0.3.0? Is this still a problem?
I also had this problem when looking at the Pretty response in Postman. It works fine If you include your return in a struct and 'stringify' the corresponding byte slice returned by json.Marshal
e.g.
type SuccessResponse struct {
Msg string `json:"msg"`
}
successObj := &SuccessResponse{
Msg: "Hello, I'm a test message",
}
resp, err := json.Marshal(successObj)
if err != nil {
return events.APIGatewayProxyResponse{}, err
}
return events.APIGatewayProxyResponse{Body: string(resp), StatusCode: 200}, nil
@dolphane The body in the response should be a string. So It seems this is working as expected.
Closing
Most helpful comment
I don't use golang myself, but I've seen this kind of message in Postman. If you are using Postman, try switching the way you look at the response body from
PrettytoRawand see if it makes a difference.