GET HTTP request payload

前端 未结 4 1690
感情败类
感情败类 2020-12-16 14:48

I am designing an API and I wonder if it is fine to send a JSON payload on a GET request?

In this other question Payloads of HTTP Request Methods, we can find accord

相关标签:
4条回答
  • 2020-12-16 15:22

    Just because you can do something, doesn't mean you should. I'm sorry if this sounds infuriatingly tautological but the thing about standards is that they're standard - and HTTP is one of the most established standards there is. It's not perfect, and there are a lot of things which a lot of people would like to change about it, but the fact that nearly everyone is still using URL parameters for use cases such as this is to me sufficient indication that there aren't any reliable alternatives right now.

    The answers from speedplane and Julian Reschke give two concrete examples of things that will break if you rely on GET requests with a body/payload. You can write your app differently to everyone else, if you want, but the web is one area where standards should perhaps be taken even more seriously than normal. I know it's tempting to be a trailblazer, but with all due respect, consider just how many websites exist, and how many web programmers there are building and maintaining them. If there was a definitively better way, you'd most likely see it widely used in production by now.

    Standards change/are adopted slowly because so many people have to agree on them for them to work. You're correct in saying that there are applications that break the rules, but you'll notice they've caused headaches for people, and workarounds/redundancy measures are required in certain cases, as mentioned by Aetherus in their comment on another answer. I tend to take the path of least resistance on issues like this. If you really want to do it, I'm sure you can make it work, though.

    0 讨论(0)
  • 2020-12-16 15:29

    Google App Engine, a popular web framework, uses a special url fetch library, which does not support making HTTP GET requests with a payload. Accordingly, if you want your API to reach Google App Engine users, then I would not recommend requiring this behavior.

    I've opened an issue regarding this with google.

    0 讨论(0)
  • 2020-12-16 15:37

    Having the GET response vary based on the request body will break caching. Don't go there.

    0 讨论(0)
  • 2020-12-16 15:46

    Also see: HTTP GET with request body - for more detail on this.

    The gist is: Yes you can, but you probably shouldn't for various reasons, including:

    • You're likely ignoring HTTP spec recommendations (you probably want to POST)
    • You may introduce caching issues
    • This is not intuitive as far as REST APIs go
    0 讨论(0)
提交回复
热议问题