7

I have a photo app (React Native) that is attempting to make a POST request to a nodejs express endpoint, with the photo and some metadata. The node app uploads the photo to s3.

Using multer, the photo + s3 bits work swimmingly, but I just can't seem to access the metadata. It's coming across empty.

Client: React Native

var formData = new FormData();

formData.append('photo', {
  uri: this.state.photo.uri,
  name: 'image.jpg',
  type: 'image/jpeg',
});

formData.append('meta', {
  title: "the best title",
  lat: this.state.lat,
  long: this.state.long
});

const config = {
  method: 'POST',
  body: formData,
  headers: {
    'Accept': 'application/json',
  }
}

console.log(config) // I see both photo and meta in the formData
fetch("http://localhost:5001/upload", config)
  .then((responseData) => {
    console.log('awesome, we did it');
  })
  .catch(err => {
    console.log(err);
  });
}

Server: Nodejs + multer + s3

const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
multerS3 = require('multer-s3');

var AWS = require('aws-sdk');
var fs =  require('fs');

var s3 = new AWS.S3();
var myBucket = 'my-bucket';
var myKey = 'jpeg';

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: myBucket,
    key: function (req, file, cb) {
      console.log(file);
      cb(null, file.originalname);
    }
  })
});

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.post('/upload', upload.array('photo', 1), (request, response, next) => {
  // The upload to s3 works fine
  console.log(request.body); // I cannot see anything in the body, I only see { meta: '' }
  response.send('uploaded!')
});

exports.app = functions.https.onRequest(app);
user2755635
  • 117
  • 1
  • 4

3 Answers3

1

It seems that you forgot to setup application to parse data that is sent as form-data. If you check out bodyparser documentation you can find out that you have to enable form-data parsing with:

app.use(bodyParser.urlencoded({
    extended: false
}));

So configuration should look like this:

const app = express();
app.use(bodyParser.urlencoded({
        extended: false
}));
app.use(bodyParser.json());

app.post('/upload', upload.array('photo', 1), (request, response, next) => 
{
  // The upload to s3 works fine
  console.log(request.body); // I cannot see anything in the body, I only see { meta: '' }
  response.send('uploaded!')
});

With this setup your code should work as expected.

Ivan Vasiljevic
  • 3,985
  • 1
  • 25
  • 33
1

try deleting 'Content-Type': 'multipart/form-data'.

the multipart/form-data type requires boundaries to be set.

You setting the Content-Type header overrides the boundary part, which should automatically be created by browser.

jujiyangasli
  • 332
  • 3
  • 12
0

Fix found here. It wasn't an issue with node or multer, as I suspected. I was incorrectly formatting the form data.

Needs to be:

formData.append('meta.title', "the best title")
formData.append('meta.lat', this.state.latitude)
formData.append('meta.long', this.state.longitude)
user2755635
  • 117
  • 1
  • 4