tanweerdev

tanweerdev

Phoenix wrapping array request inside _json field

Phoenix automatically wraps array request inside a _json field and make it an object. eg I am sending below body

 [
    {
        "activity_model_id": "{{activity_model_id}}",
        "context": "scan shop ABC",
        "card_id": "{{card_id}}"
    },
    {
        "activity_model_id": "{{activity_model_id}}",
        "context": "checking home",
        "card_id": "{{card_id}}"
    }
]

when I inspect/print raw body via a middleware(just for debugging purpose), raw body is correct and matched which is

" [\n    {\n        \"activity_model_id\": \"{{activity_model_id}}\",\n        \"context\": \"scan shop ABC\",\n        \"card_id\": \"73c0da33-89d1-4f2f-9af1-3e282c3a9606\"\n    },\n    {\n        \"activity_model_id\": \"{{activity_model_id}}\",\n        \"context\": \"checking home\",\n        \"card_id\": \"73c0da33-89d1-4f2f-9af1-3e282c3a9606\"\n    }\n]"

But what I actually see in the logs which is being sent to controllers is

Parameters: %{"_json" => [%{"activity_model_id" => "{{activity_model_id}}", "card_id" => "73c0da33-89d1-4f2f-9af1-3e282c3a9606", "context" => "scan shop ABC"}, %{"activity_model_id" => "{{activity_model_id}}", "card_id" => "73c0da33-89d1-4f2f-9af1-3e282c3a9606", "context" => "checking home"}]}

You can see, just before sending body to params, extra _json field is introduced from somewhere.
I also have this config:

config :phoenix, :json_library, Jason

So either this field is introduced by Jason or phoenix itself. currently having no clue, how to get correct request without _json field in controllers. any help is really appreciated. Thanks

Marked As Solved

josevalim

josevalim

Creator of Elixir

You cannot because conn.params is a map. So if we set it to a list, other parts of plug will fail. But the list is store as is under _json. You can also disable Plug.Parsers.JSON and roll your own parser.

Also Liked

NobbZ

NobbZ

This is not phoenix doing it, it is documented behaviour of Plug.Parsers.JSON:

JSON documents that aren’t maps (arrays, strings, numbers, etc) are parsed into a "_json" key to allow proper param merging.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
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
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

jerry
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
_russellb
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
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement