Search code examples
javascripttypescriptfacebook-graph-api

How can I post a photo and caption in the same publish on a facebook group api using FormData and axios?


Someone could help me, I'm trying to send a photo and caption into a group, but isn't working! I'd like send the photo that I recieve as base64 and send to a facebook group api.

What I'm doing? I got the base64 then convert into buffer and write it on a local disk, then I read it into a formdata. I load the data into a form this way =>

const form = new FormData();
    const fileContent = Buffer.from(url as any, 'base64');

    fs.writeFile('../tmp', fileContent, (err) => {
      if (err) return console.log(err)
    })


form.append('groupId', groupId)
 form.append('caption', caption) 
form.append('image ', fs.createReadStream('../tmp'))

In below is the axios configurations and request

 await client.post(`${config.facebook.BASE_URL}/${groupId}/photos`, form, {
  headers: {
    ...form.getHeaders(),
    Authorization: `OAuth ${config.facebook.token}`,
    'content-type': 'multipart/form-data',
    file_type: "image/jpeg",
  }

})

Note: This way I got the Error: Request failed with status code 500


Solution

  • I already resolved this although I changed de way that the file came to me, instead of receiving the image in base64 I'm receiving a signed url from google storage, and on the form.append, it's necessary to pass the name and the extension of the file, like this => form.append('source', image, 'file.jpg'); of course together with the other params and axios' configurations