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

upload multipart form data #29

Open
noprobz09 opened this issue Jun 19, 2023 · 7 comments
Open

upload multipart form data #29

noprobz09 opened this issue Jun 19, 2023 · 7 comments

Comments

@noprobz09
Copy link

Is there a sample of how to upload a file with multipart form data header? Thanks!

@cvalues
Copy link

cvalues commented Jun 22, 2023

same question..

const formData = new FormData();
          formData.append('image', data.image);
          formData.append('title', data.title);
          formData.append('body', data.body);

    $apiFetch(`api/url`, {
      method: 'POST',
      baseURL: config.public.API_BASE_URL,
      headers: {
        'Content-Type': "multipart/form-data"
      },
      params: formData

does not send the file

@cvalues
Copy link

cvalues commented Jun 22, 2023

encode file to base64 works, but only if filesize is under 30kb, why?
FetchError: Failed to fetch

@KarsjenKoop
Copy link

You could try to cast the image to a blob data type. But I am not sure if it will work.

@kreejzak
Copy link
Collaborator

Hello everyone, you should be able to send multipart form data just like in ofetch that this module uses under the hood.
I found one issue that mentions sending formData using ofetch here.

it mentions putting formData vartiable into Content-Disposition header.

const formData = new FormData()

formData.append('avatar', avatar)
formData.append('model', model)
formData.append('id', id)

$fetch('/avatar', {
  method: 'POST',
  body: formData,
  headers: { 'Content-Disposition': formData }
})

Please do let me know if this works. As I'm working on major release of this package and this totally should be topic mentioned in future documentation. Thanks

@cvalues
Copy link

cvalues commented Jun 22, 2023

yes, this works! thank you!

@noprobz09
Copy link
Author

noprobz09 commented Jun 23, 2023

Its not working this side. Below is my sample code

const file = event.target.files[0];
        const formData = new FormData()

        formData.append('file', file)
        
        $fetch($config.nuxtSanctumAuth.baseUrl + '/api/v1/attachments', {
            method: 'POST',
            body: formData,
            headers: { 'Content-Disposition': formData }
        })

The return status from the server is Status 419.
Suggestions are much appreciated. Thanks!

@kreejzak
Copy link
Collaborator

kreejzak commented Jul 3, 2023

Hello @noprobz09,
in the example you have provided you are not using the $apiFetch method provided by this module.
If you are trying to upload a file on the guarded route, you have to use $apiFetch instead of $fetch.

const { $apiFetch } = useNuxtApp()

//.... your code

const response = $apiFetch('/api/v1/attachments', {
    method: 'POST',
    body: formData,
    headers: { 'Content-Disposition': formData }
})

Please let me know if it helped.
Thanks

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

4 participants