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

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement