最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Send data with PUT-Request - Stack Overflow

programmeradmin2浏览0评论

I want to send data with a PUT request.

I have an input field for the data to send. with submit I want to send the data.

my data are in JSON format.

This is only for a pentest - so it has to be a PUT Request

Here is the code:

<header>
<meta charset="UTF-8">
<script src=".1.1/jquery.min.js"></script>

<script type="javascript">

$(document).ready(function(){

$(#submit).click(){
    var xmlHttp = getNewHTTPObject(); //returns a XMLHttpRequest object
    function chargeURLPut(url) { 
        var mimeType = "text/plain";  
        xmlHttp.open('PUT', url, true);  // true : asynchrone false: synchrone
        xmlHttp.setRequestHeader('Content-Type', mimeType);  
        xmlHttp.send($(#data).val(); 
}}});

</script>
</header>
<body>
  <form>
    Data:<br>
    <input id="data" type="text" name="data" value="Mickey"><br>
    <input id="submit" type="submit" value="Submit">
 </form>
</body>
</html>

But the data are added to the URL like GET-Parameters. After the HTTP header, I want data like "{date: foo....}"

Here is an example Request below:

PUT /foo/bar/v1/users HTTP/1.1
Host: www.foo.bar
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: /frontend/
Content-Type: application/json;charset=UTF-8
Authorization: eyJhbGciOiJSUzUxMiJ9.eyJzdW
origin: 
Content-Length: 598
Connection: close

{foo: bar, foo:bar}

I want to send data with a PUT request.

I have an input field for the data to send. with submit I want to send the data.

my data are in JSON format.

This is only for a pentest - so it has to be a PUT Request

Here is the code:

<header>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="javascript">

$(document).ready(function(){

$(#submit).click(){
    var xmlHttp = getNewHTTPObject(); //returns a XMLHttpRequest object
    function chargeURLPut(url) { 
        var mimeType = "text/plain";  
        xmlHttp.open('PUT', url, true);  // true : asynchrone false: synchrone
        xmlHttp.setRequestHeader('Content-Type', mimeType);  
        xmlHttp.send($(#data).val(); 
}}});

</script>
</header>
<body>
  <form>
    Data:<br>
    <input id="data" type="text" name="data" value="Mickey"><br>
    <input id="submit" type="submit" value="Submit">
 </form>
</body>
</html>

But the data are added to the URL like GET-Parameters. After the HTTP header, I want data like "{date: foo....}"

Here is an example Request below:

PUT /foo/bar/v1/users HTTP/1.1
Host: www.foo.bar
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: */*
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://www.foo.bar/frontend/
Content-Type: application/json;charset=UTF-8
Authorization: eyJhbGciOiJSUzUxMiJ9.eyJzdW
origin: https://www.foo.bar
Content-Length: 598
Connection: close

{foo: bar, foo:bar}
Share Improve this question edited Sep 21, 2019 at 16:05 James-Jesse Drinkard 15.7k16 gold badges118 silver badges140 bronze badges asked Jan 16, 2017 at 16:29 spitzbuaamyspitzbuaamy 7712 gold badges11 silver badges26 bronze badges 4
  • 1 Okay so what have you tried and whats the problem? – Craicerjack Commented Jan 16, 2017 at 16:32
  • When you try your JS, what kind of problem do you have? – kevingreen Commented Jan 16, 2017 at 16:41
  • 1 I'm surprised that JavaScript even runs with type="javascript". – Ultimater Commented Jan 16, 2017 at 16:42
  • 1 There's a missing parentheses on the send. xmlHttp.send($(#data).val(); should be xmlHttp.send($(#data).val()); – kevingreen Commented Jan 16, 2017 at 17:00
Add a comment  | 

2 Answers 2

Reset to default 5

You need to send the data as x-www-form-urlencoded

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

And you have to call the defined function inside the .click()

$(#submit).click(){
    var xmlHttp = getNewHTTPObject(); //returns a XMLHttpRequest object
    chargeURLPut('url');

    function chargeURLPut(url) { 
        var mimeType = "text/plain";  
        xmlHttp.open('PUT', url, true);  // true : asynchrone false: synchrone
        xmlHttp.setRequestHeader('Content-Type', mimeType);  
        xmlHttp.send($('#data').val()); 
    }
});

You could try using the $.ajax call.

<html>

<head>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#submit').click(  function () {
                console.log("click");
                var sendData = $('#data').val();

                $.ajax({
                    url: 'localhost',    //Your api url
                    type: 'PUT',   //type is any HTTP method
                    data: {
                        data: sendData
                    },      //Data as js object
                    success: function () {
                    }
                })
                ;

            });
        });
    </script>
</head>
<body>
<form>
    Data:<br>
    <input id="data" type="text" name="data" value="Mickey"><br>
    <input id="submit" type="button"  value="Submit">
</form>
</body>
</html>

I tested this on a webserver. It works.

发布评论

评论列表(0)

  1. 暂无评论