Express: GET method and request.body

Created on 10 Dec 2014  路  7Comments  路  Source: expressjs/express

Hello everyone,

I am using a GET method

according to http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Safe_methods

I want to access on request.body. How to do it?

I am using body-parser and my request body is always empty!

tnkx

question

Most helpful comment

In a GET request parameters are added to the end of the path in the following format:
?field=value&field2=value2

In this case you should use:

req.query

Or am I wrong?

Pd: Sorry for my English.

All 7 comments

It works perfectly fine with the GET method, I use it all the time. Please show a console.dir(req.headers) of the GET request that you are sending a body with, and I can see what is going wrong.

In a GET request parameters are added to the end of the path in the following format:
?field=value&field2=value2

In this case you should use:

req.query

Or am I wrong?

Pd: Sorry for my English.

Both methods work. Both req.body and req.query can be used with _all_ methods.

@Fabryprog I'm going to close this issue since I haven't head back yet. Please feel free to still reply and I can always re-open it :)

Yeah. I had control it and Maybe it isn't an express problem but client problem!

Specifically, I have not managed to send a GET request with a body payload.

I am using jquery or similar client!

Tnkx

I am using jquery or similar client!

Yes, I don't believe jQuery allows you to do this, at least, I don't know a way, since what you put in data for a GET request ends up in req.query instead of req.body due to how jQuery works.

From http://api.jquery.com/jQuery.ajax/ :

It's appended to the url for GET-requests.

@Fabryprog You can not send bodies with HTTP GET or HEAD requests in the browser. The body will always be discarded.

Edit: Relevant specification: https://xhr.spec.whatwg.org/#the-send()-method. See "If the request method is GET or HEAD, set body to null."

Was this page helpful?
0 / 5 - 0 ratings