# Post param parsed as integer

**URL:** <https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989>\
**Category:** Questions\
**Tags:** phoenix, params\
**Created:** [January 21, 2021, 9:22am UTC](https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989 "2021-01-21T09:22:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![augnustin](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/augnustin/32/14538_2.png) [@augnustin](https://forum.elixirforum.com/u/augnustin)\
**Post date:** [January 21, 2021, 9:22am UTC](https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989/1 "2021-01-21T09:22:20Z")

</div>

So I have my form with those fields POSTed:

```elixir
utf8: ✓
_csrf_token: a1ILBA0eGSJSJggHJ25GYj8IIEACShkC8bgoudFN8DcUCC01EIT2Mxjl
_method: put
certification[id]: 10
certification[is_active]: true
certification[rncp_id]: 367
certification[acronym]: Diplôme
certification[level]: 7
commit: Update Certification

```

But on the server side, once the params are parsed, I get the following map:

```elixir
%{
  id: 10,
  is_active: true,
  rncp_id: 367,
  acronym: "Diplôme",
  level: "7"
}

```

`:id` is an integer so I’m happy it was parsed, but neither `:rncp_id` nor `:level` are. Why does `:rncp_id` gets parsed as integer while `:level` correctly won’t?

I’m guessing this is because its name finishes with `_id`, but could this any how make sense here?

This breaks my changeset afterwards, with some: `errors: [rncp_id: {"is invalid", [type: :string, validation: :cast]}]` error. ☹

Why is this happening? Why such subjective rule if so? Anyway the fields are `cast`ed afterwards so it wouldn’t matter to keep it as a string.

Anyway, I’ll go for a dirty:

```elixir
    params = case params[:rncp_id] do
      v when is_integer(v) -> Map.put(params, :rncp_id, Integer.to_string(v))
      _ -> params
    end

```

for now.

Thanks

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [January 21, 2021, 9:40am UTC](https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989/2 "2021-01-21T09:40:50Z")

</div>

Where do you assert the parsed params and how do you trigger it (test or browser)? Generally form params are not type converted at all. Casting them with ecto is the only thing doing that.

---

<div class="post-metadata">

**Author:** ![dimitarvp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dimitarvp/32/38664_2.png) [@dimitarvp](https://forum.elixirforum.com/u/dimitarvp)\
**Post date:** [January 21, 2021, 10:29am UTC](https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989/3 "2021-01-21T10:29:39Z")

</div>

You likely have something like `Ecto.Changeset.cast(..., params["certification"])` in your controller, or in a plug. And it’s likely casted to an Ecto model which has these rules.

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [January 21, 2021, 12:11pm UTC](https://forum.elixirforum.com/t/post-param-parsed-as-integer/36989/4 "2021-01-21T12:11:13Z")

</div>

> [@augnustin](#):
>
> once the params are parsed

I’d look for where this is happening - the shape you showed (atom-keyed map) isn’t the default behavior for Phoenix (string-keyed map). Maybe there’s a plug in the router or the endpoint that’s doing additional parsing?
