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 file with file;filename (multiple parameters) #197

Closed
jaimesemp opened this issue Aug 3, 2023 · 2 comments
Closed

Upload file with file;filename (multiple parameters) #197

jaimesemp opened this issue Aug 3, 2023 · 2 comments

Comments

@jaimesemp
Copy link

jaimesemp commented Aug 3, 2023

Hi, thanks a lot for this library.

I have been using it deeply, but I have some struggles with multipart/forms.

I want to change this part of the result request:

Content-Disposition: form-data; name="file"

to:

Content-Disposition: form-data; name="file"; filename="datasheet5.pdf"

I have tried the FormDataAddon, and the .body(formData) methods, but I have been unable to add this second parameter, in fact, I do not have any clue at all of how to achieve it.

I am using wretch in my custom library (I want to use it on backend & frontend)

Could any give me some hints?

Thanks in advance

@jaimesemp
Copy link
Author

jaimesemp commented Aug 3, 2023

Finally I did.

I add here some minimum example (I was not able to do it using FormDataAddon):

import * as fs from "fs"
import * as path from "path"
import FormData from 'form-data'; // this is the same library than wretch use, so i guess it should not be any problem

const duckImagePath = path.resolve(__dirname, "duck.jpg") // just place a duck.jpg in your working folder
const duckImage = fs.readFileSync(duckImagePath)

export function postFormData() {
    const formData = new FormData(); 
    formData.append('file', duckImage, {filename: 'little-duck.jpg'});
    return wretch('https://anyurlyouwant.free.beeceptor.com')
        .headers(formData.getHeaders())
        .url('/files')
        .body(formData.getBuffer())
        .post()
        .res(console.log)
        .catch(console.error)
}

postFormData()

Result:

`
----------------------------834223576204015562841633
Content-Disposition: form-data; name="file"; filename="little-duck.jpg"
Content-Type: image/jpeg

�����JFIF������C�������������������������� �
(more bytes here)
----------------------------834223576204015562841633--
`

Maybe, a little more documentation or examples about this topic would be great

@elbywan
Copy link
Owner

elbywan commented Aug 5, 2023

Hey @jaimesemp,

I have tried the FormDataAddon, and the .body(formData) methods, but I have been unable to add this second parameter, in fact, I do not have any clue at all of how to achieve it.

As you noticed, the FormData addon is just syntactic sugar around FormData.append(…). Since it serializes an object (key/value) it is impossible to specify the filename as an additional argument.

Finally I did.

Glad you nailed it in the end!

Maybe, a little more documentation or examples about this topic would be great

I will add a little something in the addon documentation 👍.

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

No branches or pull requests

2 participants