tags:

views:

38

answers:

3

One of our most common error situations in a web application is that a user executes a GET request where the form should have submitted a POST.

I realize that the most likely case is that they got to the page (by way of a POST) and then clicked in the address bar and hit enter.

The question is, are there other less obvious ways that this could happen (print previews/back button, etc)?

We have never been able to consistently repeat the problems. The problems for different users and different pages nor do they happen very often (maybe once a week).

EDIT: There is no data submitted with the GET request and therefore the processing page crashes out.

A: 

Wrong, the most obvious reason why you get a GET instead of a POST is that because you're doing a GET instead of a POST.

A less obvious reason is you forgot to specify method="post" in one of your forms. The HTML standard specifies that the default method is GET, so if you want to POST, you must specify method="post" explicitly.

Scrutinize all of your tags and make sure all of them explicitly specify method="post".

EDIT: When you click on the address bar and pressed enter, yes it's true that the browser will GET a page, but your form wouldn't be submitted that way since the browser treats the URL similar to how a copy-pasted URL would be: a new session without any additional information sent to the server (except cookies).

Lie Ryan
Thanks. The point I didn't make clearly was that 99% of the time all of the pages work (POST) without flaw. It's only "Randomly" that the pages are called with a GET and I generally assume it's the user hitting enter in the address bar. I'm trying to make sure that assumption is valid.
Tom Hubbard
@Tom Hubbard: try validating your failed page, you probably accidentally generated an invalid markup that confused the browser under very specific circumstances, and makes the browser does not correctly parse the method="post". Note that such invalid markup could be on any parts of the page, and possibly even outside the relevant <form> tag. Try to pass for 100% validity.
Lie Ryan
A: 

As you said your problem is intermittent, so having a problem in form method set as get instead of post can be overruled but yes you are right, that if user presses enter in address bar it would be a get request and back button request always depends upon the last request made, if it was a post then any good browser will prompt you about resubmission and if it was get then no prompt, page will be bought back(may be from cache).

May be you can use Firebug (track requests in .net tab)or Fiddler and do some tests with different users/pages if you can reproduce it, its simply pressing enter in address bar.

Edit: And also get is always supposed to 'retrieve information' so if browser is missing something or need something it will be a get but again check in IIS log for those get requests and try them in browser,if they contains query string for viewstate and eventvalidation, then they are really mis-formed request from post to get, if form method is not explicitly set to get.

SSA
A: 

I believe that an answer to the question "what are reasons for a browser executing GET rather than POST" does not help to solve the problem of receiving a GET on a form where you expect the a GET. But: To answer that question I would simply say that you receive an GET because the browser sends an GET and you can send a GET on any page where you can send a POST. On the other hand, if the user uses the form the browser sends a POST. Thus your server has to be prepared to handle the GET and it should handle the GET in the same manner as a POST with invalid parameters. My suggestion:

  • If you receive a GET, display the form again.
  • If you receive a POST with invalid data, display the form again and notify the user that he has to enter the data in a specific way.

Maybe a trivial answer, but that's what I would do. Don't know if it adds to the discussion.

Christian Fries