jeudi 2 avril 2015

How to check if there is any payload (POST) data in a request with hapijs?

This is what I have:



server.route({
method: 'POST',
path: '/somePath/',
config: {
payload:{
output: 'stream'
}
});


I expect a short XML and that is why I using 'stream' instead of 'data'. I am using FakeToe as the XML parser as it is stream based and in my handler function I call request.payload.pipe(parser). All I want to check is if there is any POST data in the request and if not send a bad request back to the user.


I have thought about getting the size of data in the request.payload stream but there does not seem to be straightforward of doing that without pulling first and then pushing back data into the stream, which I don't like.


Everything works perfectly when the data is there, but if not FakeToe does not call the callback function with an error saying that the stream is empty. Also, I do not want to call request.payload.read method myself just to check if there is any data.


Any way to do that in hapijs?


Aucun commentaire:

Enregistrer un commentaire