简体   繁体   中英

Can't read form data from HttpServletRequest

I have a simple xmlHttpRequest that submits a DELETE to a servlet.

Here is the JS:

        function sendDELETE () {
            var http = new XMLHttpRequest();
            var url = "myServlet";
            var params = "param="+document.getElementById("param").value;
            http.open("DELETE", url, false);

            //Send the proper header information along with the request
            http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4 && http.status == 200) {
                }
            }
            http.send(params);
        }

This invoked by an onClick event on a button.

Here is what chrome dev tools show:

Request URL:http://xxxxxx/myServlet
Request Method:DELETE
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:36
Content-type:application/x-www-form-urlencoded
Host:xxxxxxxxx
Origin:http://xxxxxxx
Referer:http://xxxxxxxxxxx/main.html
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
Form Dataview parsed
param=value


Response Headersview source
    Connection:Keep-Alive
    Content-Length:0
    Content-Type:text/html
    Date:Wed, 27 Aug 2014 01:50:10 GMT
    Keep-Alive:timeout=65, max=7998
    Server:Apache-Coyote/1.1

By looking at this i can clearly see that the form data has the param=value in the request going OUT

On my server in the doDelete I tried two ways:

String param = req.getParameter("param"); and

String inputMap = CharStreams.toString(req.getReader()); String [] parts = inputMap.split("="); String param = parts[1];

But neither works - the 'param' remains null....

What am i doing wrong ?

Only POST request have parameters as an entity in the request body. You must send it as part of the URL.

See RFC 2616 : "The DELETE method requests that the origin server delete the resource identified by the Request-URI".

From what I can see, you aren't doing anything wrong at all.

In HTTP, any request can have a request body (see http://svn.tools.ietf.org/svn/wg/httpbis/specs/rfc7230.html#message.body.length ). Also, HTTP doesn't care about the type of the payload.

Whether it makes sense to send a payload for DELETE is a separate question; after all, the resource to be deleted needs to be identified by the request URI anyways.

If you can't retrieve the request body in the payload this might be caused by either a bug in the servlet implementation (which is it?), or something between your client and server (proxy? servlet filters?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM