登录
首页 >  Golang >  Go问答

解析负载与“无服务器调用本地”不同,与在 APIGateway 后面部署时不同

来源:stackoverflow

时间:2024-04-16 20:30:33 467浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《解析负载与“无服务器调用本地”不同,与在 APIGateway 后面部署时不同》,聊聊,我们一起来看看吧!

问题内容

无服务器使用 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) ,
 multivalueheaders: (map[string][]string) ,
 querystringparameters: (map[string]string) ,
 multivaluequerystringparameters: (map[string][]string) ,
 pathparameters: (map[string]string) ,
 stagevariables: (map[string]string) ,
 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 {}) ,
  httpmethod: (string) "",
  apiid: (string) ""
 },
 body: (string) "",
 isbase64encoded: (bool) false
}

只有当您使用数据类型作为请求参数时才能提取它,如下所示...

func Handler(ctx context.Context, request SearchQuery) (Response, error)

有没有办法让这两个环境同样工作?


解决方案


我通过使用 --path arg 而不是 --data arg 并像这样写出有效负载来实现此工作。

serverless 调用本地 -f myfunc --log --path mydata.json

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

终于介绍完啦!小伙伴们,这篇关于《解析负载与“无服务器调用本地”不同,与在 APIGateway 后面部署时不同》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>