4

I am currently developing a API which should receive an image's base64, but when I try to pass this into the payload and send by POST, the Server (which by the way is made with Node, using express and body-parser as middleware) gives me the following error:

request entity too large

After searching a lot in the web, I found people saying that I should use:

bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.use(bodyParser.json({limit: '50mb'}));

intead of using:

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

However, by doing so, I receive this error:

Unexpected token i in JSON at position 5

Does anyone know what I am missing here? Thanks in advance!

This is a print with the object I am trying to send as an example:

Object with base64 attribute

Matheus Minguini
  • 147
  • 1
  • 13
  • Are you sending the image in the body as raw text, or wrapped in a JSON object? – Joe Clay Aug 10 '17 at 13:19
  • @JoeClay, I am sending as a JavaScript Object (JSON), because I'm using Angular 2 in the app's frontend – Matheus Minguini Aug 10 '17 at 13:23
  • Okay, that rules out you having a missing `bodyParser` middleware. Can you add the contents of your request (or the code snippet that generates the JSON object) to the question? It seems like the JSON you're sending is malformed. – Joe Clay Aug 10 '17 at 13:27
  • @JoeClay of course, I put an object in the question as example – Matheus Minguini Aug 10 '17 at 13:52

2 Answers2

4

Thank you all that helped me out on this issue! For those who are facing or will face this, I was able to solve that by passing an object to the bodyParser.urlencoded method and BodyParser.json method.

Since they accept an object in their methods, we can pass the limit and the size of the request, So I configure mine doing like that:

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
    limit: '5mb',
    parameterLimit: 100000,
    extended: false 
}));

app.use(bodyParser.json({
    limit: '5mb'
}));

There is this topic here which can help as well:

Issue on Stack

There is also the documentation of BodyParser itself, which you can acess by clicking documentation about BodyParser

Matheus Minguini
  • 147
  • 1
  • 13
1

Try swapping bodyParser.json and bodyParser.urlencoded.

var bodyParser = require('body-parser'); app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({limit: '50mb', extended: true}))

Bharathvaj Ganesan
  • 2,562
  • 1
  • 14
  • 30