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

91,573

Solution 1

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.

Solution 2

I have a feeling from your question you were just hoping to send a post request in a browser's address bar.

Just type the following into the address bar swapping the value for 'action' to the url that you like.

data:text/html,<body onload="document.body.firstChild.submit()"><form method="post" action="http://stackoverflow.com">

It's invalid html, but the browser's (at least all the ones i've tested it in so far) know what you mean, and I wanted to keep it as short as I could.

If you want to post values, append as many inputs as you like, swapping name and value in each input for whatever you like.

<input value="[email protected]" name="email">
<input value="passwordsavedinhistory" name="password">

It's important to note that sensitive information you post will be visible in:

  • your history
  • your address bar
  • your browser's autocomplete.
  • possibly other sites that you visit from the same tab
  • probably plenty of other things too

It's a really bad way to send a post request, and all the other answers are far better, but it's still cool that you can do it.

Solution 3

<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.

Solution 4

you can not force any web browser to send the url with POST header. But to test a POST request url , I can suggest "POSTER" extention of chrome and mozilla

Solution 5

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.

Share:
91,573
Daniel Kivatinos
Author by

Daniel Kivatinos

I'm the COO and co-founder of drchrono. drchrono is an electronic medical records platform for physicians and patients. Looking to change the world through hacking and technology? We are hiring at drchrono. drchrono.com/careers Take our hacker challenge Learn more here about our hackathons - iOS Engineer Django Engineer If you're also interested in leveraging an API in healthcare, you can learn more here - drchrono.com/api

Updated on July 09, 2022

Comments

  • Daniel Kivatinos
    Daniel Kivatinos almost 2 years

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

    • randomx
      randomx over 14 years
      do you mean from a form or a href?
    • NotMe
      NotMe over 14 years
      What are you really trying to accomplish with this? a "post" is typically used to send some data from the client to the server. a "get" is to retrieve data.. but the browser natively sends some data for those, like cookies, browser info, etc.
    • zardilior
      zardilior over 7 years
      I am having the same problem i post to a url using xmlhttprequest and it does a get
  • Daniel Kivatinos
    Daniel Kivatinos over 14 years
    There is no way to POST in a URL location bar?
  • Asaph
    Asaph over 14 years
    @Daniel: I updated my answer to provide a hacky way of submitting a post form from a link.
  • Rosdi Kasim
    Rosdi Kasim over 10 years
    I know this is an old answer, but may I know why you say it is 'not recommended' to force a link submit a form?
  • Asaph
    Asaph almost 9 years
    @RosdiKasim I know this is an old comment but it's not recommended because it breaks the semantics of the world wide web. Links are expected to be idempotent GET requests. Making them POSTs violates that expectation. One of the ways this breaks the web is for search engine crawlers which try not to submit forms so they don't cause databases to be updated, etc.
  • Ed Bayiates
    Ed Bayiates almost 6 years
    Ryan White's answer worked sending a POST from the location bar.