Krt_Malta
Krt_Malta

Reputation: 9465

Specifying parameter in PUT - Rest Web Service

I'm building a REST web service using the Jersey API and I've having a problem passing the parameter to the PUT method. The method is this:

@PUT
@Consumes("text/html")
public void putHtml (String content) {
    System.out.println("Content"+content);
}

I'm calling it using the following:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript">
            function passName()
            {

                xmlHttp=new XMLHttpRequest();
                var url = "/RestWebApp/resources/greeting";
                xmlHttp.open("PUT",url,false);
                xmlHttp.send();
            }
        </script>
        <title>REST Testing</title>
    </head>
    <body>
        Put name:<input type="text" name="name"/>
        <br />
        <input type="button" value="Put" onclick="passName()"/>
    </body>
</html>

The PUT method is being called since I'm getting "Content " printed in the server console (which is Glassfish) but the parameter is not being read it seems. Is there an @statement or something which I should add to the parameter?

Thanks! Krt_Malta

Upvotes: 0

Views: 1089

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142014

You are not setting the body of the request in your client code.

You need to send some content, e.g.

xmlHttp.Send("here is my content")

However you have declared your receiving endpoint to accept "text/html". I would change that to accept "text/plain" instead. Or if you really want to receive multiple values you could accept "application/x-www-urlencoded-form". However you will need to format that yourself in the javascript. Web browsers don't know how to PUT forms. They can only POST forms.

Upvotes: 1

Related Questions