Axios POST request 413 (Payload Too Large)

Viewed 15637

This is an axios POST request in my React project. labelIDs is a long array whose length is around 8000.

    axios.post('http://localhost:3000/api/filter', {
      'ids': labelIDs,
      'like_ratio_range': ["0", "1"]
    })

I've tried body-parserbut it didn't solve the problem. Any idea how to config axios post body limit?

Or should this API be better designed?

1 Answers

The solution to this problem lies within body parser and how your middleware is set up. Body-parser's limit option will only take in effect if your content-type and type option match. It looks like you're sending regular json to the server, your bodyparser middleware should look something like this.

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json({limit: '100kb'}));

If the payload is still too large, increment the bodyParser.json() limit option. Try '200kb', try '300kb'. 100kb shoudld be enough though.

Related