Httpoison with client certification throws error that gives no info

Im trying to implement the folowing call in Elixir:

curl -q -k --cert client-2048.crt --key client-2048.key
https://identitysso.betfair.com/api/certlogin -d
"username=testuser&password=testpassword" -H "X-Application:
curlCommandLineTest"

This is what I have so far:

defmodule BfgEngine.Client.Betfair do
alias BfgEngine.API.Betfair.{Login}
def login(%{username: username, password: password, certs: certs}) do
body = %{username: username, password: password}
Login.post("/certlogin", body)
|> IO.inspect()
end
end

defmodule BfgEngine.API.Betfair.Login do
use HTTPoison.Base

@base_url " https://identitysso.betfair.com/api"

@expected_fields ~w(sessionToken loginStatus)

def process_url(resource) do
@base_url <> resource
end

def process_request_headers(_headers) do
[
“X-Application”: “1”,
“content-type”: “application/x-www-form-urlencoded”,
“Accept”: “Application/json; Charset=utf-8”
]
end

def process_response_body(body) do
body
|> Poison.decode!()
|> Map.take(@expected_fields)
|> Enum.map(fn {k, v} -> {String.to_atom(k), v} end)
end

def process_request_body(%{username: username, password: password}) do
“username=#{username}&password=#{password}”
|> URI.encode_www_form()
end

def process_request_options(_options) do
[
ssl: [
certfile: “/certs/client-2048.pem”,
keyfile: “/certs/client-2048.key”
]
]
end
end

However in the repl im getting the following error:

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> BfgEngine.API.Betfair.Login.start
{:ok, []}
iex(2)> BfgEngine.Client.Betfair.login(%{password: “///////”, username: “/////”, app_key: “//////”, certs: “”})
** (exit) :badarg
(kernel) gen_tcp.erl:153: :gen_tcp.connect/4
(hackney) /Users/henriklarsson/repos/elixir/bfg_engine/deps/hackney/src/hackney_connect.erl:247: :hackney_connect.do_connect/5
(hackney) /Users/henriklarsson/repos/elixir/bfg_engine/deps/hackney/src/hackney_connect.erl:37: :hackney_connect.connect/5
(hackney) /Users/henriklarsson/repos/elixir/bfg_engine/deps/hackney/src/hackney.erl:316: :hackney.request/5
(httpoison) lib/httpoison/base.ex:630: HTTPoison.Base.request/9
(bfg_engine) lib/bfg_engine/betfair.ex:27: BfgEngine.Client.Betfair.login/1

Now my questions are:

  1. What causes this error.
  2. Is this a good way of implementing a rest client?
  3. Are there any other problems with this implementation to get it working in the same way as the curl?
1 Like

Hi!

Can you please format your code snippets by using ``` blocks?

```
like this   
```

This means a bad argument was given to the socket, so a bad URL most likely.

A space at the front of the url is not valid, so this is most likely the cause. :slight_smile:

1 Like

You should keep the post, such that it helps in the future with similar problems. I’d be happy if you could revert your changes and format your original text that it gets displayed properly.

2 Likes

If my post fixed it you should mark it as the solution, you’d be surprised at how often something like URL formatting can trip up people. :slight_smile:

EDIT: Awesome thanks, now it is more easily searchable! :smiley:

1 Like