Alexandre S Hostert
Alexandre S Hostert

Reputation: 340

Issues with HTTPoison.post

I use Poloniex API to make my trades for long time. My previous version was coded in bash shell script. Now, I'm doing the code in Elixir.

The issue I'm facing now is related to API refusing my commands. Look this test. Nonce will be repeated just to see if the same signature is generated.

Bash

tapiurl: https://poloniex.com/tradingApi
postdata: command=returnBalances&nonce=1455742931958817
sign: 340970c8dc8b2f50c2772b2edf45297d7c3758c922192a5fbbae82c70b3b408dab5f652d23e25a8e6bf7fd687ff18f02f36ffbc5b71d496298af33c5ffc73291
command: curl -iv -H "Key: $apikey" -H "Sign: $sign" -d "$postdata" $tapiurl
Result is okay:
{"1CR":"0.00000000","ABY":"0.00000000"....

Now, look the same postdata and sign on Elixir.

Elixir

tapiurl = "https://poloniex.com/tradingApi"
nonce = :os.system_time(:micro_seconds)
data = %{command: "returnBalances", nonce: nonce}
postdata = URI.encode_query(data)
sign = Base.encode16(:crypto.hmac(:sha512, apisecret, postdata), case: :lower)
headers = [Key: apikey, Sign: sign]
HTTPoison.post(tapiurl, postdata, headers)

Result: "{\"error\":\"Invalid command.\"}"

As you can see, I'm sending the same data in both languages, so why I'm getting "Invalid command"? The same nonce is just an example, to check if the same signature is being generated. I know I have to use always different nonces.

Upvotes: 2

Views: 2569

Answers (2)

Roman Smirnov
Roman Smirnov

Reputation: 533

Try

postdata = [command: "returnBalances", nonce: nonce]
HTTPoison.post(tapiurl, {:form, postdata})

Besides some API endpoint expect JSON-encoded string instead of separate post params. In that cases Poison.encode!(postdata) could be used.

Upvotes: 2

Gazler
Gazler

Reputation: 84180

The Poloniex API expects the content-type header to be set.

Try this:

headers = [Key: apikey, Sign: sign, "Content-Type": "application/x-www-form-urlencoded"]

Upvotes: 1

Related Questions