2017-05-12 59 views
1

我有一個API的例子curl命令:如何翻譯捲曲長命仙丹httpoison

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

我不知道如何翻譯成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)> 

你能幫我嗎?我試圖把數據放在身體部分而不是頭部,但沒有運氣。

回答

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

感謝Dogbert,可惜沒有奏效。我得到一個錯誤,說'**(Poison.SyntaxError)意外的輸入結束位置0 (毒)lib/poison/parser.ex:55:Poison.Parser.parse!/ 2 (毒)lib /毒。例如:83:Poison.decode!/ 2 (httpoison)lib/httpoison/base.ex:471:HTTPoison.Base.response/6 (blockcypher)lib/blockcypher/handler.ex:47:Blockcypher.Handler.post_transa ction_new/4' –

+0

@Dogbert在原始請求中,'input'和'outputs [addresses + value]'都是_arrays_。我相信將地圖封裝成'[]'應該可以使其工作。 – mudasobwa

+0

@mudasobwa謝謝!我用'Poison.decode!'解碼了原始字符串並複製粘貼它。希望現在沒有錯誤! @LegitStack你可以嘗試我的新代碼? – Dogbert