Can't access `request._body` in `server.onrequest()`
I have a simple server and I need to access the request._body property. Unfortunately it always defaults to an empty string. The client does not have access to the response body either. My code snippets are below. Any help is appreciated.
Thanks
//server.js
const runtime = require('runtimejs')
const http = require('eshttp')
const server = new http.HttpServer()
const response = new HttpResponse(200, {'x-header': 'value'}, 'hello from the server')
server.onrequest = request => {
console.log('request body', request._body) // prints 'request body: '
request.respondWith(response)
}
server.listen(9000)
my client looks like this.
// client.js
const http = require('eshttp')
const HttpClient = http.HttpClient
const HttpRequest = http.HttpRequest
const client = new HttpClient('localhost', 9000)
const request = new HttpRequest('GET', '/', {'x-header': 'value'}, 'hello from the client')
client.request(request, (err res) => {
if (err) console.log(err)
console.log('response:', res._body) // prints 'response: undefined'
})
client.close()
@austinfrey Request body isn't (yet) supported, unfortunately. There will be a way to attach ondata and onend handlers to the request to collect all pieces of the body.
@iefserge that explains it :) thanks for the explanation 👍
@iefserge Is this something you'd accept a PR request for? If you point me in the right direction, I could work on it.
@austinfrey yeah, that would be great!
This is where incoming data chunks are pushed into the parser https://github.com/iefserge/eshttp/blob/master/lib/http-request.js#L131.
Parser extracts body (if exists) from the request and stores it in _lastBodyChunk. This happens every time new chunk is parsed https://github.com/iefserge/eshttp/blob/master/lib/http-parser.js#L59
Those buffers can be then forwarded to ondata handler. Also we'll need to call onend when streaming is complete.