tags:

views:

161

answers:

3

How do you force a web browser to use POST when getting a url?

+5  A: 

Use an HTML form that specifies post as the method:

<form method="post" action="/my/url/">
    ...
    <input type="submit" name="submit" value="Submit using POST" />
</form>

If you had to make it happen as a link (not recommended), you could have an onclick handler dynamically build a form and submit it.

<script type="text/javascript">
function submitAsPost(url) {
    var postForm = document.createElement('form');
    postForm.action = url;
    postForm.method = 'post';
    var bodyTag = document.getElementsByTagName('body')[0];
    bodyTag.appendChild(postForm);
    postForm.submit();
}
</script>
<a href="/my/url" onclick="submitAsPost(this.href); return false;">this is my post link</a>

If you need to enforce this on the server side, you should check the HTTP method and if it's not equal to POST, send an HTTP 405 response code (method not allowed) back to the browser and exit. Exactly how you implement that will depend on your programming language/framework, etc.

Asaph
+5  A: 
<form method="post">

If you're GETting a URL, you're GETting it, not POSTing it. You certainly can't cause a browser to issue a POST request via its location bar.

Jonathan Feinberg
There is no way to POST in a URL location bar?
Daniel
Will you believe me if I say it twice?
Jonathan Feinberg
@Daniel: I updated my answer to provide a hacky way of submitting a post form from a link.
Asaph
+1  A: 

If you're trying to test something, I'd suggest using Fiddler to craft your http requests. It will allow you to specify the action verb (GET, POST, PUT, DELETE, etc) as well as request contents. If you're trying to test a very specific request from a link but with POST instead, then you can monitor the requests your browser is making and reissue it only with the modified POST action.

dserink