12

I'm writing a server app and wanted the client to use data in body to pararmeterize my GET method, like this:

# http -v GET http://localhost:3000/url text=123 foo=bar
GET /url HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 29
Content-Type: application/json; charset=utf-8
Host: localhost:3000
User-Agent: HTTPie/0.4.0

{
    "foo": "bar", 
    "text": "123"
}

In AngularJS I tried:

var params = {
    "foo": "bar", 
    "text": "123"
}

// no body
$http({
  method: 'GET',
  url: '/url',
  data: params })

// ugly url
// also has its limitation: http://stackoverflow.com/questions/978061/http-get-with-request-body
$http({
  method: 'GET',
  url: '/url',
  params: params })

// params in body, but I wanted GET
$http({
  method: 'POST',
  url: '/url',
  data: params })

Is this by design or a bug?

I cannot see why from the documentation.

leesei
  • 6,020
  • 2
  • 29
  • 51
  • 3
    Are you sure the receiving end supports it? [GET requests with bodies are not forbidden, but not expected either](http://stackoverflow.com/questions/978061/http-get-with-request-body). – bzlm Apr 25 '13 at 08:54
  • 2
    Forget the docs, go research what GET means in http. – 7stud Apr 25 '13 at 08:54
  • GET method does not support request body – Arun P Johny Apr 25 '13 at 08:55
  • 5
    I concur... The answer to your question is "use POST instead" - but as it's not a true answer, I'm not risking my rep on it. – boisvert Apr 25 '13 at 08:58
  • In short it is not possible using GET, if you want to sent the json data as request body I suggest you use PUT or POST – Arun P Johny Apr 25 '13 at 09:00
  • Somehow [related question](http://stackoverflow.com/questions/16201929/angular-content-type-is-not-being-generated-correctly-when-using-resource/16203589#16203589) – garst Apr 25 '13 at 09:03
  • @bzlm and gargc, thanks for the links. I wrote the server so it will definitely supports it :-P. I also edited the post to add this info in, – leesei Apr 25 '13 at 15:36
  • I would take [this](http://stackoverflow.com/a/15656884/665507) as the answer: it's not prohibited but you shouldn't use it as the server may ignore it. – leesei Apr 25 '13 at 15:52
  • The best explanation I have seen is [this answer](https://stackoverflow.com/a/37037066/6440354) – thprogrammer Jun 08 '16 at 12:44

1 Answers1

15

I would take this as the answer:

For HTTP, it's not prohibited, but you shouldn't use it as the server may (and SHOULD) ignore the body of GET request.

Reference: HTTP GET with request body

For XHR, body of GET and HEAD will be ignored (hinted by @jacob-koshy).

Reference: https://xhr.spec.whatwg.org/#the-send()-method

Community
  • 1
  • 1
leesei
  • 6,020
  • 2
  • 29
  • 51