Search code examples
resthttptagswebapi

HTTP TAG - "GET" with body or "POST" to get data?


I'm building a Web API and I have a method that have the purpose of get data to the screen, and this methods have to many parameters, so to simplify and facilitate my code, I just added an object to the method, and now my get method have a body. I read that it's wrong to have a body in a get request. But if I change this method to "post", it will not be fulfilling its role, because I'm not changing nothing in the database or something like that, I'm just bringing data to the screen.

What is the best choice in my case?


Solution

  • What is the best choice in my case?

    Today, your best choice is to express the information that needs to be sent to the server within the identifier of the resource, and use GET. For inspiration, think "web search form".

    When that's not possible (server programmer convenience is a weak excuse, but sometimes the amount of information means that the identifier exceeds the defacto length limits of the web), then you should use POST. See Fielding 2009

    POST serves many useful purposes in HTTP, including the general purpose of "this action isn’t worth standardizing."

    In essence, all of the other HTTP methods are specializations of POST, available to express the fact that there are additional constraints in play.

    So you can always fall back to POST when the other options aren't appropriate.


    At some point in the future, QUERY (aka GET-with-a-body) will be a registered HTTP method, and that will be a close fit for what you want.

    (Some folks closer to the action have stated that it is "safe" to start using the QUERY method now; but we don't yet have a published standard, the token isn't registered, the most recent draft of the standard expired months ago, so I'm not comfortable making that recommendation myself.)