代码之家  ›  专栏  ›  技术社区  ›  Pentium10

云工作流:TypeError:字典键必须是字符串;得到:int

  •  0
  • Pentium10  · 技术社区  · 4 年前

    我们有一个云工作流配方设置,如下所示:

    - readWikipedia:
        call: http.post
        args:
            url: https://api-ssl.bitly.com/v4/shorten
            headers:
                Authorization: "Bearer <token>"
                Content-type: "application/json"
            body:
                domain: bit.ly
                long_url: https://cloud.google.com/blog
        result: bitresult
    - returnResult:
        return: ${bitresult.body[0]}
    

    我们得到一个错误: TypeError: dictionary key must be a string; got: int

    这是我们得到的完整回应:

    argument: 'null'
    endTime: '2020-10-16T17:40:17.595823493Z'
    error:
      context: in step "returnResult"
      payload: '{"message":"TypeError: dictionary key must be a string; got: int","tags":["TypeError"]}'       
    name: projects/985596417983/locations/us-central1/workflows/http_post/executions/5bfc56c0-b498-4149-9d28-2c0d51295a9a
    startTime: '2020-10-16T17:40:17.297154818Z'
    state: FAILED
    workflowRevisionId: 000006-e18
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   Pentium10    4 年前

    当你遇到错误时,你需要阅读错误消息。

    它为您提供 context: in step "returnResult"

    这个错误意味着 ${bitresult.body[0]} 位置 0 输出中不存在。

    Bit.ly返回一个json文档,如下所示:

    {
       "archived":false,
       "created_at":"2020-10-16T17:40:17+0000",
       "id":"bit.ly/35452TM",
       "link":"//bit .ly/35452TM",
       "long_url":"https://cloud.google.com/blog/",
    }
    

    所以它没有 body[0] 相反,它有 body.link

    推荐文章