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

4
  • Are you sending the image in the body as raw text, or wrapped in a JSON object?
    – Joe Clay
    Aug 10, 2017 at 13:19
  • @JoeClay, I am sending as a JavaScript Object (JSON), because I'm using Angular 2 in the app's frontend Aug 10, 2017 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, 2017 at 13:27
  • @JoeClay of course, I put an object in the question as example Aug 10, 2017 at 13:52

2 Answers 2

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

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}))

4
  • Do you know if I have to use JSON.stringfy in the object and send this String to the Backend? Aug 10, 2017 at 14:16
  • Are you sending json response? Aug 10, 2017 at 14:27
  • If yes you need to send it as string and parse it @client side Aug 10, 2017 at 14:32
  • I stringfied the object before sending to the Backend, but when it arrives there, it consoles an empty Object, do I need to parse the 'request.body'? If so, would you know how? Aug 10, 2017 at 16:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.