Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$request is empty when using multipart/form-data #19

Open
YoussF opened this issue Oct 3, 2018 · 1 comment
Open

$request is empty when using multipart/form-data #19

YoussF opened this issue Oct 3, 2018 · 1 comment

Comments

@YoussF
Copy link

YoussF commented Oct 3, 2018

Hello,

I've been working with jeffrey's Form.js for a while now without any issue. Until i had to add a input type file in my registration form for the user to upload an avatar while creating the account. So i had to adapt a little bit the form.submit() so that i can tell axios to use "multipart/form-data" like so :

let config = { headers: { 'Content-Type': 'multipart/form-data' } }
axios[requestType](url, this.data(), config)

The minute i've done that, the hole form stopped working. I had tried to troubleshoot the issue by doing a dd($request). And now i seems like $request is empty which was before that change to axios, filled with all the form fields as before.

So i asked our friend Google, and i found this interesting issue laravel/framework#13457 which seems to be somehow related.

To be honest, i didn't quite understood why framework/issues/13457 status is closed. The workaround doesn't seems to be working. The only difference between the reported issue and my case, is that i use the post method to submit the form as they are using put/patch

I really tried my best to resolve this issue by my own. But i can't and now honestly i'm stuck :(

@saulchelewani
Copy link

saulchelewani commented Jan 3, 2019

In my case I passed the payload as a FormData object which worked. I created a setFormData method

setFormData(data) {
  let formData = new FormData();
  for (let field in data) {
    formData.append(field, data[field]);
  }
  return formData;
}

and the submit method becomes

submit(requestType, url) {
  return new Promise((resolve, reject) => {

    // pass the response from setFormData() as payload

    axios[requestType](url, this.setFormData(this.data()))
      .then(response => {
        this.onSuccess(response.data);

        resolve(response.data);
      })
      .catch(error => {
        this.onFail(error.response.data);

        reject(error.response.data);
    });
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants