REST API: GET request with body

匆匆过客 提交于 2019-11-28 13:16:47

As a general rule, the idea of a GET in REST is that any of your parameters are sent in the URL. As the answer on the question you included indicates, it's doable but misses the point of REST, which is to have a consistent webbish interface. If you want to pass complex data to your endpoint, you probably want to use a POST, which your users will expect to have a body for. I'd highly recommend reconsidering that implementation.

But to your actual question, sure there are clients that can't send a body on a GET. Mostly I'd imagine your clients will be programmatic, say, python's urlib2, and while you can set a body on GET, it is not really the intended use of the module, so you're forcing the programmer to get weird. More importantly, the idea of the REST api is to be client-agnostic, which is why it sounds to me like your API design should be reworked here.

Regfor

It's a bad idea to use body in GET HTTP requests. Yes, seems to be that "de jure" HTTP GET can have body, but "de facto" you will be have problems:

  1. With client frameworks/libraries. It will be hard to find support of it.
  2. Server can just ignore the body of GET Request. And anyway it's not standard way, and could be problems with server or it's configuration.
  3. It makes your code, especially on server side, unclear to others, because nobody will expect GET with body.

Are you looking for hard way? With GET with body you will got so many pitfalls. Why not to use other HTTP Verb?

For example use POST (or some other verbs), than:

  1. it's easy to have already ready client library,
  2. no problems with server or server configuration,
  3. it's clear to other

Do not look for harder ways :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!