sijibomi

sijibomi

%Plug.Conn.Unfetched{aspect: :body_params} in body of post request

Hi everyone. I’m trying to fetch the body of a post request and I keep getting Plug.Conn.Unfetched
This is what I have in my route handler

import Plug.Conn
  use Plug.Router 
  plug(Plug.Parsers,
    parsers: [:json],
    pass: ["text/*"],
    json_decoder: Poison
  )
  plug(:match)
  plug(:dispatch)

  post "/login" do
    with %{"email" => email, "password" => password} <- conn.body_params do
      changeset= Login.changeset(%{
        "email"=> email, "password" => password
      })
      case Login.execute(changeset) do
        {:ok, lgn}->
          conn
          |> put_resp_content_type("application/json")
          |> send_resp(
            200,
            Poison.encode!(%{data: lgn})
          )

        _->
          conn
          |> put_resp_content_type("application/json")
          |> send_resp(
            200,
            Poison.encode!(%{error: "invalid login details"})
          )
      end

    else
      _ ->
        conn
        |> put_resp_content_type("application/json")
        |> send_resp(
          400,
          Poison.encode!(%{error: "invalid input"})
        )
    end
  end

I’m tying to run the test below and it keeps failing

test "trade username and password for access tokens and refresh tokens", t do

      {:ok, resp_body} = HttpRequest.post("/api/auth/login", %{"email"=> t.user.email, "password"=> "NNNdnsfsndkn???234."})
      IO.inspect(resp_body)
      assert is_binary(resp_body["accessToken"])
      assert is_binary(resp_body["refreshToken"])
    end

HttpRequest is an test helper I wrote to send http request

defmodule Test.HttpRequest do
  def post(path, body, opts \\ []) do
    callers =
      self()
      |> :erlang.term_to_binary()
      |> Base.encode16()
    IO.inspect(body)
    case :post
         |> Finch.build(
           "http://localhost:4001" <> path,
           [{"content-type", "application/json"}, {"user-agent", callers}] ++ opts,
           Jason.encode!(body)
         )
         |> Finch.request(HttpRequests) do
      {:ok, %Finch.Response{body: body, status: 200}} ->
        {:ok, Jason.decode!(body)}

      x ->
        x
    end
  end
end

Most Liked

ruslandoga

ruslandoga

You’d need to add Plug.Parsers — Plug v1.20.2 to the plug pipeline.

Last Post!

guitcastro

guitcastro

I had the same issue, in my case I was adding Plug.Parsers after my router plug, Changing the order fix it.

Where Next?

Popular in Questions Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement