Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BodyParser: Unexpected Token -

I'm actually developping an Angular webform with a file upload and some data. Here are the request headers:

POST /tests/add HTTP/1.1
Host: localhost:3000
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Content-Type: application/json;charset=utf-8
Referer: http://localhost:8000/
Content-Length: 2033

I build the request this way:

var formData = new FormData();
formData.append('file', $scope.test.file);
$http({
  method: 'POST',
  url: backUrl + '/tests/add',
  data: { 'file': $scope.file,
          'token': 'test'},
  contentType: false,
  processData: false,
  transformRequest: function (data, headersGetter) {
      var formData = new FormData();
      angular.forEach(data, function (value, key) {
          formData.append(key, value);
      });
      var headers = headersGetter();
      delete headers['Content-Type'];
      return formData;
  }

But it always return a 400 bad request with this error:

Unexpected token -
400

SyntaxError: Unexpected token -
    at parse (/home/me/projects/www/node_modules/body-parser/lib/types/json.js:83:15)
    at /home/me/projects/www/node_modules/body-parser/lib/read.js:116:18
    at invokeCallback (/home/me/projects/www/node_modules/raw-body/index.js:262:16)
    at done (/home/me/projects/www/node_modules/raw-body/index.js:251:7)
    at IncomingMessage.onEnd (/home/me/projects/www/node_modules/raw-body/index.js:307:7)
    at emitNone (events.js:86:13)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

This is the only reference to bodyParser I have in my backend:

app.use(bodyParser.json());       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true,
  defer: true
}));

What am I doing wrong ?

like image 395
Papotitu Avatar asked Nov 02 '16 13:11

Papotitu


People also ask

Do I need to install bodyParser?

No need to install body-parser with express , but you have to use it if you will receive post request.

What is the alternative of bodyParser?

node. js - Non-deprecated alternative to body-parser in Express.

What does bodyParser text () do?

bodyParser. text([options]) Returns middleware that parses all bodies as a string and only looks at requests where the Content-Type header matches the type option. This parser supports automatic inflation of gzip and deflate encodings.

Is bodyParser deprecated?

'bodyParser' is deprecated. // If you are using Express 4.16+ you don't have to import body-parser anymore.

What is the use of bodyparser?

bodyParser.json([options]) Returns middleware that only parses json and only looks at requests where the Content-Type header matches the type option. This parser accepts any Unicode encoding of the body and supports automatic inflation of gzip and deflate encodings.

What is urlencoded body parser?

bodyParser.urlencoded([options]) Returns middleware that only parses urlencoded bodies and only looks at requests where the Content-Type header matches the type option. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of gzip and deflate encodings.

What is a buffer object in request body?

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body ). This will be a Buffer object of the body. The raw function takes an optional options object that may contain any of the following keys:

Why am I getting an entity parse failed error?

This error will occur when the request contained an entity that could not be parsed by the middleware. The status property is set to 400, the type property is set to 'entity.parse.failed', and the body property is set to the entity value that failed parsing.


1 Answers

Are you positive you're sending JSON?

Most likely you are using invalid JSON which forces error with bodyParser.json().

Make sure you are using valid JSON. You can also swap bodyParser.json with bodyParser.raw for middleware.

like image 166
defaultcheckbox Avatar answered Nov 15 '22 03:11

defaultcheckbox