无服务器使用invoke local处理请求有效负载的方式与部署到AWS时的行为似乎有所不同。

预期的行为是您将像这样将有效负载从请求主体中拉出...

func Handler(ctx context.Context, request events.APIGatewayProxyRequest)

然后像这样将其解码为您的数据类型...
type SearchQuery struct {
    Caller string `json:"caller"`
    Value  string `json:"value"`
}
err := json.Unmarshal([]byte(request.Body), &searchQuery)

但是,当使用--data标志在本地调用功能时,有效负载不在request.Body中。
events.APIGatewayProxyRequest) {
 Resource: (string) "",
 Path: (string) "",
 HTTPMethod: (string) "",
 Headers: (map[string]string) <nil>,
 MultiValueHeaders: (map[string][]string) <nil>,
 QueryStringParameters: (map[string]string) <nil>,
 MultiValueQueryStringParameters: (map[string][]string) <nil>,
 PathParameters: (map[string]string) <nil>,
 StageVariables: (map[string]string) <nil>,
 RequestContext: (events.APIGatewayProxyRequestContext) {
  AccountID: (string) "",
  ResourceID: (string) "",
  Stage: (string) "",
  RequestID: (string) "",
  Identity: (events.APIGatewayRequestIdentity) {
   CognitoIdentityPoolID: (string) "",
   AccountID: (string) "",
   CognitoIdentityID: (string) "",
   Caller: (string) "",
   APIKey: (string) "",
   AccessKey: (string) "",
   SourceIP: (string) "",
   CognitoAuthenticationType: (string) "",
   CognitoAuthenticationProvider: (string) "",
   UserArn: (string) "",
   UserAgent: (string) "",
   User: (string) ""
  },
  ResourcePath: (string) "",
  Authorizer: (map[string]interface {}) <nil>,
  HTTPMethod: (string) "",
  APIID: (string) ""
 },
 Body: (string) "",
 IsBase64Encoded: (bool) false
}

仅当您将数据类型用作请求参数时才可以提取它。
func Handler(ctx context.Context, request SearchQuery) (Response, error)

有没有办法使这两个环境相同?

最佳答案

我通过使用--path arg而不是--data arg来工作,并像这样写出有效负载。
serverless invoke local -f myfunc --log --path mydata.json

{
    "body": "{\"caller\": \"foo\", \"value\": \"[email protected]\"}"
}

关于go - 与 `serverless invoke local`相比,解析有效负载与在APIGateway后面部署时有所不同,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57581616/

10-10 16:46