0

I have created an endpoint to upload an avatar with my nodeJS API.

The endpoint works fine when I use it from POSTMAN.

But it doesn't work when I call it with Fetch from my react-js app.

When I use Fetch to call the endpoint, the busboy.on('file', () = {...}) seems to be not triggered.

The fetch method in my react-js app:

export const fetchPostFile = async (url, file) => {

  try {

    const data = new FormData();
    data.append("image", file);
    
    const rawResponse = await fetch(url, {
      method: 'POST',
      body: data
    });

    return await rawResponse.json();

  } catch (error) {

    console.error(error);
    return {success: false, message: 'query_can_not_be_sent'};

  }

}

The input formData sent by reactjs:

------WebKitFormBoundary5WI3GuEx81A7nLq0
Content-Disposition: form-data; name="image"

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/7QL6UGhvdG9zaG9wIDMuMAA4QklNBAQAAAAAAt0cAQAAAgACHAEUAAIAARwBFgACAAIcAR4ACUFGUC1QSE9UTxwBKAAIMTIzNDU2NzgcATwAATUcAUYACDIwMjAxMDExHAFQAAsxNTU5MjcrMDAwMBwBWgADGy1BHAFkAApBRlBfOFJWOUFQHAIAAAIAAhwCBQAZVEVOTklTLUZSQS1PUEVOLU1F...

The api method to upload the avatar

exports.postAvatar = async (request, response) => {

  response.set('Access-Control-Allow-Origin', '*');
  response.set('Access-Control-Allow-Headers', '*');

  const BusBoy = require('busboy');
  const fs = require('fs');
  const os = require('os');
  const path = require('path');

  const busboy = new BusBoy({ headers: request.headers });
  
  busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
    console.log('busboy.on(file)');
  });

  busboy.on('finish', async () => {
    console.log('busboy.on(finish)');
  });

  busboy.end(request.rawBody);

  return response.json({message: 'ok'});

}

busboy.on(file) message is not displayed when I call from reactjs but it is displayed with a postman call.

EDIT:

I checked the POSTMAN input and the image is not sent as base64 but like this:

postman body

In Postman, I tried with a base64 picture in a field text in input and I have the same result that with Fetch.

Is busboy can receive a base64 encoded image ? Or can I convert the base64 picture to a file input ?

Paul
  • 1,290
  • 6
  • 24
  • 46
  • You need to provide a [mcve] (where is the server side code?). If it works with Postman, what is different about the requests? Are any errors reported by the browser or the server? – Quentin Oct 17 '20 at 07:56
  • Are you getting a preflight related CORS error in the browser console? – Quentin Oct 17 '20 at 15:10
  • @Quentin I added the api method, no error message (server side or client side). I can't determine the differences between the postman call and the fetch call, so i'm little bit confuse – Paul Oct 17 '20 at 15:10
  • @Quentin no cors error, I have a good 200 OK for the call to POST api/avatar – Paul Oct 17 '20 at 15:16
  • If you have problems with postman/code then just use postman to generate the client code ;P It supports javascript – Toumash Feb 16 '22 at 19:01

0 Answers0