Search code examples
httppostgetxmlhttprequest

Can HTTP POST Be Used to Get Data -- Not Create New Data


Two things:

First, am I correct that an HTTP POST can be used to retrieve existing information? If so, what is the response code?

Second, if POST can be used, what id the format of the URL in a Web.API application, and what data should be sent to the server.

Company security does not prevent using HTTP Get, but they strongly discourage it because of some sort of security issues. OTOH, I really dislike naming a method PostInformation(), when I want to GET existing information.

Thanks


Solution

  • am I correct that an HTTP POST can be used to retrieve existing information?

    From RFC 7231, section 4.3.3:

    The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.

    This implies that the server changes state while not strictly requiring it. So yes, while not really encouraged, this is safe to do. As a matter of fact, many web applications have done so in the past to circumvent limitations of the GET method such as overly long URLs.

    what [is] the format of the URL in a Web.API application, and what data should be sent to the server.

    The URL would be the same sans the query string. If you mark the body of your request being application/x-www-form-urlencoded, you can fill it with the same string you would normally use for the query string. For more complex or binary data, you should use multipart/formdata (see this answer).

    I really dislike naming a method PostInformation(), when I want to GET existing information

    That is pretty much a non-issue. I understand your worries, but consider this: What you ultimately do is getting data. How you do so is an implementation detail of the protocol in use. Nothing should prevent you from naming your method PostInformation(). Besides, what were you to do if the implementation changes and you suddenly used GET instead of POST? Refactor all occurences of PostInformation() into GetInformation()?