lyo5ha
Hi! I want to receive GET request with JSON body.
#router
...
pipeline :api do
plug :accepts, ["json"]
end
scope "/", PsRbkWeb do
pipe_through :api
get "/order", PaymentController, :index
end
...
# controller
...
def index(conn, _some_params) do
IO.inspect(conn) # empty body_params: %{} in conn
end
...
After I made GET request to “/order” with some json in body (with header Content-Type=application/json). - have empty body_params: %{} in conn in my controller.
Here is resulting conn, sorry for length:
%Plug.Conn{
adapter: {Plug.Cowboy.Conn, :...},
assigns: %{},
before_send: [#Function<1.112466771/1 in Plug.Logger.call/2>,
#Function<0.66982185/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>],
body_params: %{},
cookies: %Plug.Conn.Unfetched{aspect: :cookies},
halted: false,
host: "localhost",
method: "GET",
owner: #PID<0.596.0>,
params: %{},
path_info: ["order"],
path_params: %{},
port: 4000,
private: %{
PsRbkWeb.Router => {[], %{}},
:phoenix_action => :index,
:phoenix_controller => PsRbkWeb.PaymentController,
:phoenix_endpoint => PsRbkWeb.Endpoint,
:phoenix_format => "json",
:phoenix_layout => {PsRbkWeb.LayoutView, :app},
:phoenix_pipelines => [:api],
:phoenix_router => PsRbkWeb.Router,
:phoenix_view => PsRbkWeb.PaymentView,
:plug_session_fetch => #Function<1.58261320/1 in Plug.Session.fetch_session/1>
},
query_params: %{},
query_string: "",
remote_ip: {127, 0, 0, 1},
req_cookies: %Plug.Conn.Unfetched{aspect: :cookies},
req_headers: [
{"accept", "*/*"},
{"accept-charset", "UTF-8"},
{"accept-encoding", "gzip, deflate"},
{"cache-control", "no-cache"},
{"connection", "keep-alive"},
{"content-length", "353"},
{"content-type", "application/json"},
{"host", "localhost:4000"},
{"postman-token", "70bc9e11-1ef4-493d-a556-cf4876d28bc2"},
{"user-agent", "PostmanRuntime/7.4.0"}
],
request_path: "/order",
resp_body: nil,
resp_cookies: %{},
resp_headers: [
{"cache-control", "max-age=0, private, must-revalidate"},
{"x-request-id", "2lonnum7h1c48rmudk000gu1"}
],
scheme: :http,
script_name: [],
secret_key_base: :...,
state: :unset,
status: nil
}
So, the question is: Is it possible to receive any json data in GET request body and access it form controller? (Or I have to give up and use POST with JSON body). Please, guide me to the right direction.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lyo5ha
Not supposed to, but I like to have such option.
I found quite hacky solution:
I think to write custom plug and put this body to some custom field in
conn. By the way,Plug.Parsersdon’t parse GET requests, so no point to add custom handler for GET requests into parsers. I hope, this will help somebody.…
Also Liked
NobbZ
HTTP GET does not have a body as far as I remember…
NobbZ
Ohyeah, you are right, according to RFC 7231:
So, according to the RFC, you either should be able to access the body or never see the request.
Perhaps the pipeline doesn’t parse the body as it does not expect it? How is your plug parser configured?
Last Post!
OvermindDL1
Could be useful to PR in a configureable option for it to parse all bodies regardless of method or via a configureable set of methods?