如何将 curl 翻译成 elixir httppoison

how to translate curl to elixir httpoison

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

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)>

你能帮帮我吗?我尝试将数据放在 body 部分而不是 headers 但没有成功。

数据应该是 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}]})
)