-4

I know that there are some differences between GET and POST, like descriped in this post on StackOverflow. But what I cannot figure out is, are they interchangable, assuming that you change your code accordingly?

For instance, could you use POST to request HTML-content and display it in the browser? I am not concerned with whether or not this is best practice, only if it is possible.

Update

I should point out, that POST and GET are two different ways of sending HTTP requests. I am aware of this and I am aware that it does makes a difference which one you use. Especially when it comes to security, it is important to know the difference.

What I basically wanted to know was, if it was possible (from a technical point of view) to use POST instead of GET and vice versa. From the answers in this post, it is clear that this is possible to use either. That does not mean that it doesn't matter which one you use.

  • Are you asking if it is possible to rewrite the browser code to use POST by default? – JacquesB Sep 18 '17 at 14:15
  • 4
    Is it possible to GET data by POSTing? Yes. Is it possible to POST data by Getting? No. – Berin Loritsch Sep 18 '17 at 14:19
  • Basically I was asking what @BerinLoritsch answered. If anyone has more details on it, it would be much appreciated :-) – Noceo Sep 18 '17 at 14:21
  • Could the people who is downvoting, please leave a comment as well. Otherwise it's quite difficult to improve my questions for next time. – Noceo Sep 18 '17 at 16:25
  • 1
    @Noceo: I'm not the downvoter, but I think if you define what "interchangeable" means that would help. Not a duplicate, but definitely related: Why shouldn't a GET request change data on the server?. – Greg Burghardt Sep 18 '17 at 17:12
5

Yes given you have control over the server's handling of this. I worked with some java server code that treated both methods as equivalent. In fact, the operation is really just a string that is passed along. You can do a GOST and PET if you like. These are conventions.

While there's nothing about HTTP that prevents this kind of thing, most APIs and/or browsers will not support it.

You say you don't care whether it's a good idea or not but I think it's important to know that mixing these up can create dangerous security flaws. It's crucial that you do not use GET for anything that is not safe. Doing so can open you up to a very easily exploited vulnerability. Specifically, if you have a GET that, for example, logs in a user, simply putting the URL in the src attribute of an img tag will cause it to happen when a page containing that image is loaded. It doesn't need to be an actual image to do this. It's a really bad idea to go against the grain with this kind of thing. It's like driving on the wrong side of the road on expressway. No good will come of it.

  • Thanks. I am well aware of the security issues, but it's a good point, since other people might not be. My interest in the topic was more academic than practical :-) – Noceo Sep 18 '17 at 15:59
2

If you look at the accepted answer on that same question and the purpose behind them, no they are not interchangeable. In fact, your JavaScript libraries won't allow you to send JSON or XML directly to a GET url endpoint.

The intent behind GET is to retrieve data. You can use it to allow searching, etc. However, the intent behind POST is to send data from the client to the server.


To answer the question specifically about whether POST URLs can return results, the answer is yes. There is no technical limitation that prevents a POST call from retrieving information from a server. For better or worse, many document databases with a built in JSON query language behave exactly in this manner.

Now there are further technical limitations based on firewall configurations that you may or may not be aware of, as well as limitations in the JavaScript API layer.

  • Some corporate firewalls that are very concerned about people accidentally spilling company secrets can turn off POST requests at the firewall level. They typically leave GET open, which is why it's not a bad idea to have search boxes accessible via GET. Granted this is a small group of users in the big scheme of things.
  • JavaScript libraries do not allow you to GET with a JSON or XML payload. While "technically" possible for machine-to-machine communications, the majority (if not all) of browser implementers do not allow you to do that.
  • Thanks for pointing that out. But does POST return any content? Or does it always have to be paired with a GET, if you want to change the content after the POST? – Noceo Sep 18 '17 at 14:21
  • POST can return content. – Berin Loritsch Sep 18 '17 at 15:49
1

Yes, but… GET has limitations over POST. Since all of the data is encoded into the URL, …

  • browser and web server specific URL length limits apply (usually around 1 to 4 KB).
  • File upload (<input type="file">) is not possible.
  • All of the content, including passwords, are visible in plain text in the address bar, and browser history.
  • URLs and thus the data is likely to be logged on web servers.
  • The response is handled as the content of that URL and may be cached (i.e., it may be outdated).
  • The URL may be queried several times (like one time by the OS to determine the content type to start the corresponding application, and the application may download the URL again). If the URL causes action on your system, the action will be repeated.
  • Same for page reload and back button clicks.
  • The character set is not explicit in an URL. This may cause additional issues.
-2

Not to my knowledge, no.

A GET request retrieves information from a website, and displays it in the format that you specify. This is useful for retrieving MI data from websites.

A POST request posts data to the website. A common use of POST is to automate account creation and such like.

Hope that helps :)

  • I agree that's how they are usually used (and intended to be used). But what about when you use post in an HTML Form? That redirects you to some URL, defined by action=. But is that then a POST followed by a GET? – Noceo Sep 18 '17 at 14:19
  • Interesting - I see what you mean now! Yes, I suppose you could use a POST request like that, you could use one to in effect automate the process a user goes through to receive information on the website. But using that method to explicitly receive information from a website is contrived at best unless you have no other option. – Korthalion Sep 18 '17 at 14:23
  • 2
    I suppose it's also worth noting that you usually receive information back from the server upon a successful POST. In the example of page/account creation the executor of the request would usually receive login/URL details. – Korthalion Sep 18 '17 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.