Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't access request._body in server.onrequest() #15

Open
austinfrey opened this issue Sep 30, 2017 · 4 comments
Open

Can't access request._body in server.onrequest() #15

austinfrey opened this issue Sep 30, 2017 · 4 comments

Comments

@austinfrey
Copy link

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()
@iefserge
Copy link
Owner

iefserge commented Oct 1, 2017

@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.

@austinfrey
Copy link
Author

@iefserge that explains it :) thanks for the explanation 👍

@austinfrey
Copy link
Author

@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 austinfrey reopened this Oct 3, 2017
@iefserge
Copy link
Owner

iefserge commented Oct 3, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants