REST services - testing PUT methods in the browser

user2144555 picture user2144555 · Apr 4, 2013 · Viewed 28.1k times · Source

I've developed REST services. I can test the GET methods by the browser, or by a Client Application. But those who have PUT methods I don't know how to consume them by the browser...

For example, I have this method that turns a lamp on, after I insert the userId:

@PUT
@Path("/lampon")
@Produces({"application/json", "text/plain"})
@Consumes("multipart/form-data")
public boolean turnOnLamp(@FormParam("userId") String userId) throws Exception
{
    boolean response = new LampManager().turnOnLamp(userId);
    return response;
}

In my client application I do this, and it works:

    String webPage = "http://localhost:8080/BliveServices/webresources/services.actuators/lampon";

    URL urlToRequest = new URL(webPage);

    //Authentication

    urlConnection = (HttpURLConnection) urlToRequest.openConnection();
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setRequestMethod("PUT");
    urlConnection.setRequestProperty("Authorization", basicAuth);
    urlConnection.setRequestProperty("Content-type", "multipart/form-data");
    urlConnection.setRequestProperty("Accept", "application/json");
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("userId", "2"));

    (...)

But how can I send the userId by the browser?

Another thing, I get this message when I build my project:

SEVERE: Resource methods utilizing @FormParam and consuming "multipart/form-data" are no longer supported. See @FormDataParam.

Thanks

Answer

Zelldon picture Zelldon · Apr 4, 2013

If you want to test the REST-Webservice with your Browser you must install an plugin.

If you use Google Chrome you can install REST Console I also use these plugin to test my Webservice.

https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn

For Firefox install these REST-Client

https://addons.mozilla.org/en-us/firefox/addon/restclient/

REST CLient is also available for Safari http://restclient.net/

For Opera you can check out the Simple REST-Client

https://addons.opera.com/en/extensions/details/simple-rest-client/

For your second Question

please try as Consumes value 'application/x-www-form-urlencoded'