benonymus

benonymus

Hey besically the title says it,
I am trying to send a file to my elixir api and, i just noticed that in the router it has this plug: plug(:accepts, ["json"]), but since i am trying to send a file i am sending a multipart request not json.
Thoughts?

Showing Posts 1 to 10

McElaney

McElaney

You shouldn’t need to change anything in the Plug pipeline - the file should be included as part of the form wrapped in a Plug.Upload struct. If you’re not seeing the file in your params it might be that you’re hitting the upload limits? The blog post on uploads would have more info: https://phoenixframework.org/blog/file-uploads

benonymus

benonymus OP

no i dont think i hit it, i am trying to send the file like this :

  let data = new FormData();
        data.append("xd", {
          recording: rec,
          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,
                "content-type": "multipart/form-data"
              }
            }
          ) 

but only an empty struct %{} show up

NobbZ

NobbZ

Can you also show the code of your controller?

benonymus

benonymus OP

glady :smile:

def create(conn, %{"recording" => recording_params}) do

    with {:ok, %Recording{} = recording} <- Web.create_recording(recording_params) do
      challenge = Web.get_challenge!(recording.challenge_id)
      number_of_days_between = Date.diff(challenge.due_date, recording.inserted_at)
      calculated_score = number_of_days_between * challenge.difficulty * 100
      user = Web.get_user!(recording.user_id)
      IO.inspect(user)
      score_to_insert = user.score + calculated_score

      updated_user = %{
        score: score_to_insert
      }

      Web.update_user(user, updated_user)
      send_resp(conn, 200, [])
      # conn
      # |> put_status(:created)
      # |> render("show.json", recording: recording)
    end
  end

youcould see most of the related code in this post: Sending and recieveing a file(audio) from app to elixir api issue

im chaing the request to try to match but no success yet

NobbZ

NobbZ

And where is it you see only an empty map?

benonymus

benonymus OP

i am runnign the server with foreground command and says this

info] POST /api/recordings
[debug] QUERY OK source="users" db=0.3ms decode=0.1ms queue=0.2ms
SELECT u0."id", u0."name", u0."password_hash", u0."score", u0."avatar", u0."role_id", u0."team_id", u
0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [2]
[debug] QUERY OK source="roles" db=0.2ms queue=0.2ms
SELECT r0."id", r0."name", r0."inserted_at", r0."updated_at", r0."id" FROM "roles" AS r0 WHERE (r0."i
d" = $1) [1]
[debug] QUERY OK source="teams" db=0.2ms queue=0.5ms
SELECT t0."id", t0."name", t0."inserted_at", t0."updated_at", t0."id" FROM "teams" AS t0 WHERE (t0."i
d" = $1) [2]
[debug] Processing with Userteam1Web.RecordingController.create/2
  Parameters: %{}
  Pipelines: [:api, :jwt_authenticated]
[info] Sent 400 in 38ms
[debug] ** (Phoenix.ActionClauseError) could not find a matching Userteam1Web.RecordingController.cre
ate clause
to process request. This typically happens when there is a
parameter mismatch but may also happen when any of the other
action arguments do not match. The request parameters are:
  %{}

here at the end

benonymus

benonymus OP

ok something intersting, if i remove this: ```
"content-type": "multipart/form-data"

then this shows up, still doesnt match but it shows up:

%{"data" =&gt; %{"_parts" =&gt; [["recording", %{"_duration" =&gt; -1, "_fsPath" =&gt; "/data/user/0/com.cobrn/files/filename.mp4", "_lastSync" =&gt; -1, "_options" =&gt; %{"autoDestroy" =&gt; true}, "_path" =&gt; "filename.mp4", "_position" =&gt; -1, "_recorderId" =&gt; 0, "_state" =&gt; -2}], ["challenge_id", 1], ["user_id", 1]]}}

benonymus

benonymus OP

it very much seems like our api does not accept formdata

NeutronStein

NeutronStein

According to axios’ documentation JS part should be like this:

let data = new FormData();
data.append("recording[file]", rec);
data.append("recording[challenge_id]", 1);
data.append("recording[user_id]", 1);
data.append("recording[mod_score]", 3);
console.log(data);
axios.post(
    config.API_URL + "recordings",
    data,
    {
        headers: {
            Authorization: "Bearer " + this.props.auth.token,
            "Content-Type": "multipart/form-data"
        }
    }
);

To match your controller.

benonymus

benonymus OP

Hey really really appreciate the answer, i tried this code and got this reponse:

`%{"challenge_id" => "1", "mod_score" => "3", "user_id" => "1"}`

and a 422 response code
so it seems like the file is not there for some reason, but the request is correct

could you link that part of their docs? I can’t seem to find it :upside_down_face:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews