0

I am trying to send an image file to my backend express.js api from my react frontend. But in my console.log i just get an empty object that looks like {} on the backend side. What is wrong with react.js api call that wont let me see the file in the console.log on the backend.

//Backend API

app.post('/api/v1/media', function (req, res) {
  console.log(req.body);
  res.json('api called');
 });

//React JS api call

const files = e.target.files;
console.log(files);

    setLoading(true);
    const res = await fetch(
      'http://localhost:4000/api/v1/media',
      {
        method:"POST",
        headers: {'Content-Type':'application/json'},
        body: JSON.stringify(files[0])
      }`enter code here`
    )
    const file = await res.json();
1
  • Try adding "Accept": "application/json" Commented Oct 30, 2019 at 16:05

1 Answer 1

1

Try using

"Content-type" : "multipart/form-data"

Also Refer to this post for file uploading reference.

Sign up to request clarification or add additional context in comments.

1 Comment

ill give this a try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.