Stepan Yakovenko
Stepan Yakovenko

Reputation: 9216

how to handle xhr blob post in nodejs

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?

Upvotes: 4

Views: 8049

Answers (2)

yeiniel
yeiniel

Reputation: 2456

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.

Upvotes: 8

leobelizquierdo
leobelizquierdo

Reputation: 1668

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

Upvotes: 2

Related Questions