Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir: How to send body parameters using hackney

Tags:

erlang

elixir

I am making a post request in elixir using hackney, which appears to be functioning properly, but on the receiving service the body is empty.

Is there a hackney option that I am missing? I've tried sending the body as a charlist as well as a native string.

msg = Jason.encode!(%{message: "hello"})

{:ok, 200, _, body} = :hackney.post(
  "http://localhost:2002/api/some/endpoint",
  [
    {'Content-Type', 'application/json'}
  ],
  msg,
  [:with_body]
)

IO.inspect(Jason.decode!(body), label: "response")
# => {:ok, "ok"}
like image 420
sdc Avatar asked Oct 27 '25 04:10

sdc


1 Answers

That sent the body for me, but it appears to want "application/json" as a binary instead of a charlist. When I did a charlist I got a numeric content type header.

It's possible your server would get that content type and handle the request body in such a way it doesn't appear to be getting sent.

This is what I ended up with (I also changed the header name to a lowercase binary for consistency, but it doesn't seem necessary)

:hackney.post("http://localhost:2002/api/some/endpoint", [{"content-type", "application/json"}], msg, [:with_body])
like image 182
Brett Beatty Avatar answered Oct 30 '25 00:10

Brett Beatty