Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
128 stars 31 forks source link

Upload failed except content-type header is omitted #322

Closed ekoeryanto closed 3 months ago

ekoeryanto commented 3 months ago

https://github.com/Secreto31126/whatsapp-api-js/blob/aef2308b9c4c27d2bec914f658cd52520de5e216/src/index.ts#L617

ekoeryanto commented 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()
  }
}
Secreto31126 commented 3 months ago

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!

ekoeryanto commented 3 months ago

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

ekoeryanto commented 3 months ago

ah forget to include it on #323

Secreto31126 commented 3 months ago

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?

ekoeryanto commented 3 months ago

about "content-type"

https://stackoverflow.com/questions/74892469/adding-content-type-multipart-form-data-on-headers-on-a-post-request-showin

fyi, i tried it on my mac, ubuntu server, redhat server.

Secreto31126 commented 3 months ago

Ok, I will keep this at mind. Lets first close #323 and I will later patch this. Thanks!

ekoeryanto commented 3 months ago

Unfortunately I am facing left and right side jobs hit me. would you mind to continue the #323, please