发布到 Slack API Elixir

Posting to Slack API Elixir

我正在尝试 post 使用 HTTPotion 来 api 松弛。我遇到了 个问题,它为我指明了正确的方向,但我一直收到错误消息:

HTTPotion.Response{body: "invalid_payload",
...

我的密码是

HTTPotion.post "https://hooks.slack.com/services/T00000000/B0000000/xxxxxxxxxxxxx", [body: "{'channel': '#elixir', 'username': 'elixir stuff', 'text': '#{text}'", headers: ["Content-Type": "application/json"]]

我不确定我的负载有什么问题

任何帮助都会很棒

那是因为你的 body 无效 JSON 因为它使用单引号而不是双引号并且也没有正确转义 text 的值。您应该为此使用 JSON 编码器,例如 poison。添加 poison 作为依赖后,你可以这样做:

body: Poison.encode!(%{channel: "#elixir", username: "elixir stuff", text: text}), ...