dongpiao0731 2018-09-16 23:36
浏览 43
已采纳

使用无服务器调用功能时传递JSON

I am running a lambda function written in Go using Serverless and I want to pass a couple of parameters to it when it's invoked.

Here's the struct I created to receive the request:

type RequestStruct struct {
    StartAt int `json:"startAt"`
    EndAt   int `json:"endAt"`
}

And in the handler I'm trying to print out the values:

func Handler(ctx context.Context,request RequestStruct) (Response, error) {
    fmt.Printf("Request: %v",request)

I tried invoking it using the --raw option, so I tried doing this

serverless invoke -f orders --raw -d '{"startAt":1533513600,"endAt":1534118399}'

and I tried wrapping it in double quotes instead

serverless invoke -f orders --raw -d "{startAt:1533513600,endAt:1534118399}"

serverless invoke -f orders --raw -d "{\"startAt\":1533513600,\"endAt\":1534118399}"

I received a marshal error with all three:

{
    "errorMessage": "json: cannot unmarshal string into Go value of type main.RequestStruct",
    "errorType": "UnmarshalTypeError"
}

I'm not sure what I am doing wrong and I can find any examples for that online, there's only this serverless doc about how to do the invoke and this aws doc about how to handle the event in Go

Update I tried invoking the event from the AWS Console and it worked, so odds are the issue is in the serverless invoke command.

  • 写回答

1条回答 默认 最新

  • donglianglu8136 2018-09-17 00:30
    关注

    I found a way around this by having my JSON in a file rather than in the command itself, this doesn't solve the issue I'm experiencing in the question but it's a way to invoke the function with Json

    I added a events/startAndEnd.json file that contains my json data:

    {
        "startAt":1533513600,
        "endAt":1534118399
    }
    

    And referenced that file in the invoke command: serverless invoke -f orders --path events/startAndEnd.json

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算