简体   繁体   中英

how to translate curl to elixir httpoison

I have an example curl command from an api:

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

I have no idea how to translate this into HTTPoison for Elixir. I've been trying for hours. I can't even begin to mention all the iterations I've gone through, but here's where I'm at now:

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

unlike most everything I've tried before this actually gets to their servers and gives a response:

"{\"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)>

can you help me out? I tried putting the data in the body portion instead of the headers but with no luck.

The data should be the second argument to HTTPoison.post/2 and should be encoded to JSON. Your data is also in the wrong format.

This should work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM