I'm trying to parse an HTTP response in the form of a String or Buffer into an Object.
The result would be an Object like the native http module's response.
I tried to import the native HTTP parser, but the results were too raw for my use-case :
var HTTPParser = process.binding('http_parser').HTTPParser;
var parser = new HTTPParser(HTTPParser.RESPONSE);
parser.onHeadersComplete = function(res) {
console.log('onHeadersComplete');
console.log(res);
};
parser.execute(data, 0, data.length);
which would return something like this :
onHeadersComplete
{
headers:
[ 'X-Powered-By',
'Express',
'Content-Type',
'text/plain',
'Content-Length',
'2',
'Date',
'Sat, 19 Apr 2014 20:16:45 GMT',
'Connection',
'keep-alive' ],
statusCode: 200,
versionMajor: 1,
versionMinor: 1,
shouldKeepAlive: true,
upgrade: false
}
Two things are lacking for my use-case :
- map of header names associated with header values
parsing the response body
- Does anyone know how to achieve this ?
Thank you in advance for your help !
netmodule to send mis-constructedHTTPrequests to test HTTP servers.