4

client code:

var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.send(blob);

server code:

app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));
app.post('/frame', function (req, resp) {
    console.log(req.body);
});

this gives PayloadTooLargeError: too many parameters adding

xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');

doesn't solve the problem. Any other ideas?

1
  • Can you provide the content of blob? Feb 24, 2016 at 18:48

2 Answers 2

8
+50

Assuming your blob variable is not really url encoded form data and just any kind of content. Then at the server side you can just read the request stream as it comes in. Remember that the req variable on your http.Server.request event handler is a readable stream. This will remove any size limit imposed by the body-parser middleware. Keeping your original client code then your server code would be:

// app.use(bodyParser.urlencoded({extended: false,limit: '50mb'}));

app.post('/frame', function (req, resp) {
  req.on('readable', function(){
    console.log(req.read());
  });
});

Processing request as it is streamed in is a good idea even for structured data if the content is too big. For example in the past I had hit performance problems when i use body-parser#json middleware with big json requests and solve the issue removing the body-parser#json middleware and using oboe for parsing the streamed input.

2
  • can you provide few references for the same.
    – Richie
    Oct 25, 2018 at 9:12
  • 1
    the req param pass to request event handlers are instances of class nodejs.org/api/http.html#http_class_http_incomingmessage and as per the docs that class is a readable stream. With that info you can search for the nodes stream doc and use the readable stream api as in the example code (registering a readable event handler)
    – yeiniel
    Oct 25, 2018 at 17:25
2

Your blob variable exceed the limit size set in the server. You must set a number always bigger than the client data (blob).

the client:

var xhr = new XMLHttpRequest();
xhr.open('POST', '/frame', true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.send(blob);

the server:

// set limit with a value always bigger than the client data
var upperBound = '1gb';
app.use(bodyParser.urlencoded({extended: false, limit: upperBound}));
app.post('/frame', function (req, resp) {
    console.log(req.body);
});
1
  • Remember to set the content-type and call the appropriate bodyParser.method(). For example, if the blob contains an image, the content-type should be application/octet-stream and the bodyParser method to be called is raw(). Otherwise the server will throw errors.
    – Menixator
    Jan 26, 2017 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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