How do you force a web browser to use POST when ge

2019-01-22 06:05发布

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

标签: html url post get
11条回答
狗以群分
2楼-- · 2019-01-22 06:27

I know this question is old but someone may find this useful. You can use a command line tool like cURL (http://curl.haxx.se/) to post to a URL.

Example:

curl -v  --basic --user username:password --request POST "http://www.theurltopostto.com"
查看更多
混吃等死
3楼-- · 2019-01-22 06:37

You can use a tool to test. I'm always asking the same question as you. There is quite a few tools available online. Here is the tool that I use: http://www.hurl.it/

查看更多
▲ chillily
4楼-- · 2019-01-22 06:38

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-22 06:39

The above submitAsPost() function is a good and elegant solution but it has a problem - if the URL is too long some browsers (including Firefox and IE) will return an error. Since many of us use POST in order to bypass this very limitation, I suggest this solution:

// submit a URL using post
function submitAsPost(url) {
    var bodyTag = document.getElementsByTagName('body')[0];
    var postForm = document.createElement('form');
    bodyTag.appendChild(postForm);
    postForm.method = 'POST';

    var serverAndParams = url.split("?");
    postForm.action = serverAndParams[0];
    var params = null;
    try
    {
      var paramsAndHash = serverAndParams[1].split("#");
      params = paramsAndHash[0]; 
      var attrList = params.split("&");
      for (var i = 0; i < attrList.length; i++)
      {
        try
        {
          var keyValue = attrList[i].split("=");
          var el = document.createElement('input');
          el.type="hidden";
          el.name=keyValue[0];
          var value = keyValue[1];
          value = value.replace(/\+/g, ' ');
          el.value=decodeURIComponent(value);
          postForm.appendChild(el);
        }
        catch(error){}
      } 
    }
    catch(error){}

    postForm.submit();
    bodyTag.removeChild(postForm);
}

Tested with Firefox, Chrome and IE.

查看更多
Anthone
6楼-- · 2019-01-22 06:43

This is a little late in the game but I ran across this and found that HTML 5 made some changes. You can use the input tag to add formmethod (thus selecting post). This worked for me.

see : http://www.w3schools.com/tags/att_input_formmethod.asp

查看更多
做自己的国王
7楼-- · 2019-01-22 06:45

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="hugh.mahn@person.com" 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.

查看更多
登录 后发表回答