I have this curl.
curl -H "Content-Type: application/json" -X POST -d '{"file_server": {"method": "api/file", "parameter": [{"filepath":"tehnnhjhst", "FileBase64String":"test"}]}}' http://localhost:4000/api/some_method
But I got this JSON response:
{"errors":{"parameter":["is invalid"]}}
This is my model
schema "some_method" do
field :method, :string
field :parameter, :string
timestamps()
end
I just wanted to store the array as plain text/string to my MySql database.
Is there parse equivalent to this? or?
Any idea? Thank you.
1 Like
You’ve defined :parameter
as a :string
but you’re passing an array, as you state. I would expect there to be an error.
Are you using changesets for your model? They help validate your datatypes with some nice error messages.
1 Like
Yes im using changesets to my model.
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:method, :parameter])
|> validate_required([:method, :parameter])
end
So array in :string (varchar(255), utf8_general_ci in my MySql database) is not allowed?
1 Like
I’m confused. An array is not a string. You should get an error message.
1 Like
Thanks for your reply. So there is no way to handle array?
1 Like
Yes, but I don’t know if MySQL handles array data types.
1 Like
Thank you for your reply. I just wanted to store the “parameter” as a plain text to my database (Ignoring that it is an array).
1 Like
NobbZ
8
Then send it as string. Or maybe hook into the json decode and make it a string there. I’m not sure if that one works though.
1 Like
I just hook it in JSON encode.
params = Map.put(params, "parameter", Poison.encode!(params["parameter"])
1 Like