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
You’d need to add Plug.Parsers — Plug v1.12.1 to the plug pipeline.
1
Popular in Questions
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it.
I’m very interested in Elixir,...
New
I would like to know what is the best IDE for elixir development?
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New







