如何将curl翻译成elixir httpoison

时间:2017-05-12 04:05:17

标签: rest curl elixir httpoison

我有一个来自api的示例curl命令:

curl -sd '{"inputs":[{"addresses": ["add42af7dd58b27e1e6ca5c4fdc01214b52d382f"]}],"outputs":[{"addresses": ["884bae20ee442a1d53a1d44b1067af42f896e541"], "value": 4200000000000000}]}' https://api.blockcypher.com/v1/eth/main/txs/new?token=YOURTOKEN

我不知道如何将其转换为Elixir的HTTPoison。我一直在努力工作几个小时。我甚至不能开始提到我经历过的所有迭代,但这就是我现在所处的位置:

Connect.post( "https://api.blockcypher.com/v1/eth/main/txs/new?token=#{@token}",
              "",
              [
                { "inputs",  "{addresses, #{address_from}}"},
                {  "outputs", "[{addresses, #{address_to}}, {value, #{eth_amount}}]"}
              ]
            )

与我之前尝试过的大多数事情不同,它实际上会到达他们的服务器并给出响应:

"{\"error\": \"Couldn't deserialize request: EOF\"}"
%{"error" => "Couldn't deserialize request: EOF"}
** (FunctionClauseError) no function clause matching in Base.encode16/2
         (elixir) lib/base.ex:175: Base.encode16(nil, [])
    (blockcypher) lib/blockcypher/handler.ex:55: Blockcypher.Handler.post_transa
ction_new/4
iex(46)>
你可以帮帮我吗?我尝试将数据放在正文部分而不是标题中,但没有运气。

1 个答案:

答案 0 :(得分:2)

数据应该是HTTPoison.post/2的第二个参数,应该编码为JSON。您的数据格式也是错误的。

这应该有效:

Connect.post(
  "https://api.blockcypher.com/v1/eth/main/txs/new?token=#{@token}",
  "",
  Poison.encode!(
    %{"inputs" => [%{"addresses" => [address_from]}],
      "outputs" => [%{"addresses" => [address_to],
                      "value" => eth_amount}]})
)