Closed ekoeryanto closed 3 months ago
hmm, what about upload from remote url?
class {
async uploadMediaFromUrl(phoneID: string, url: string) {
const media = await this.fetch(url)
const blob = await media.blob()
const file = new File([blob], 'filename.pdf', { lastModified: Date.now(), type: blob.type })
const form = new FormData()
form.append('file', file)
form.append('messaging_product', 'whatsapp')
const response = await this.fetch(`https://graph.facebook.com/${this.v}/${phoneID}/media`, {
method: 'post',
headers: {
Authorization: `Bearer ${this.token}`
},
body: form
})
return response.json()
}
}
Hi!
Just to make it clear, does the code you are sharing works as expected or is it a proposed solution?
In my experience with uploading media, the File class would always fail with 400 error, most probably because the filename entry in the blob.
If there's a way to make it work as expected, I would love to add it to the documentation.
Thanks for opening an issue!
the code snippet uploadMediaFromUrl
i shared is just a quick idea came from mind, i implemented the code and works.
Sorry before for confusing thread, the first post is issue reporting as I can't upload if there is "Content-Type": "multipart/form-data"
header. if I remove it, upload works fine
ah forget to include it on #323
Don't worry, we can make a separate PR for it. I prefer smaller and cleaner PR rather than messy ones :)
About the "Content-Type" property, it would be nice to know why it's failing in your machine rather than just removing it. However, if no clear reason appears and it doesn't break the rest of the existing code, it can be patched.
And the upload from URL, although sounds like a nice feature, I don't think it is within the library's responsability to handle/seems to be out of scope. If anything, the user can implement it by itself using the already existing methods, and it will be able to handle unexpected errors, such as the url not being a media. It will be a much better developing experience to debug that rather than the minified library code 😄.
Instead, we could make it part of the documentation. The uploadMedia method already contains an example on how to upload local files, so it would be perfectly reasonable to include your code as a how to upload from an URL.
What do you think?
about "content-type"
fyi, i tried it on my mac, ubuntu server, redhat server.
Ok, I will keep this at mind. Lets first close #323 and I will later patch this. Thanks!
Unfortunately I am facing left and right side jobs hit me. would you mind to continue the #323, please
https://github.com/Secreto31126/whatsapp-api-js/blob/aef2308b9c4c27d2bec914f658cd52520de5e216/src/index.ts#L617