Hey I am trying to send an audio recording from my react-native android app the request looks like this:
let data = new FormData();
data.append("recording", {
uri: rec._fsPath,
type: "audio/mp4",
name: "testRec",
challenge_id: 1,
user_id: 1,
mod_score: 3
});
console.log(data);
axios
.post(
config.API_URL + "recordings",
{ data },
{ headers: { Authorization: "Bearer " + this.props.auth.token } }
)
.then(res => res.data);
the params when arrive they look like this:
This typically happens when there is aparameter mismatch but may also happen when any of the other action arguments do not match. The request parameters are:
%{"data" => %{"_parts" => [["recording", %{"challenge_id" => 1, "mod_score" => 3, "name" => "testR ec", "type" => "audio/mp4", "uri" => "/data/user/0/com.cobrn/files/filename.mp4", "user_id" => 1}]]} }
and my api is expecting recording_params that goes here:
with {:ok, %Recording{} = recording} <- Web.create_recording(recording_params)
that goes here:
def create_recording(attrs \\ %{}) do
%Recording{}
|> Recording.changeset(attrs)
|> Repo.insert()
end
that goes to my schema that looks like this:
defmodule Userteam1.Web.Recording do
use Ecto.Schema
use Arc.Ecto.Schema
import Ecto.Changeset
schema "recordings" do
field(:path_to_recording, Userteam1Web.Recording.Type)
field(:mod_score, :integer)
belongs_to(:user, Userteam1.Web.User)
belongs_to(:challenge, Userteam1.Web.Challenge)
has_many(:comment, Userteam1.Web.Comment)
timestamps()
end
@doc false
def changeset(recording, attrs) do
recording
|> cast(attrs, [:challenge_id, :user_id, :mod_score])
|> cast_attachments(attrs, [:path_to_recording])
|> validate_required([:path_to_recording])
end
end
as you can see it should cast the attachment, as the documentation and examples showed for ARC and ARC-ecto but either the request is wrong in some form, or is it not attached, does append do what attach wants to take?
I know that this might be a lot to read, but I would really appreciate some help with this issue, and I think this could be good resource for other devs too in the future.