6

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

user2144555
  • 1,315
  • 11
  • 34
  • 55
  • Try to use one of the following: RESTClient or Poster they both plugin to firefox browser or download free version of SoapUi. They all great for testing web service and simple to use. – justMe Apr 04 '13 at 13:32

2 Answers2

9

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'

Community
  • 1
  • 1
Zelldon
  • 5,396
  • 3
  • 34
  • 46
  • The REST-Client seems really nice. I'll try now use the 'application/x-www-form-urlencoded' – user2144555 Apr 04 '13 at 14:15
  • But I continue using the `@FormParam`? Because the error is still appearing. – user2144555 Apr 04 '13 at 16:01
  • @PUT @Path("/lampon") @Produces({"application/json", "text/plain"}) @Consumes("application/x-www-form-urlencoded") public boolean turnOnLamp(@FormParam("userId") String userId) throws Exception { boolean response = new LampManager().turnOnLamp(userId); return response; } should work... – Zelldon Apr 04 '13 at 16:42
  • Yes, that was I did, but I keep receiving `Resource methods utilizing @FormParam and consuming "multipart/form-data" are no longer supported. See @FormDataParam.`... I change it in every method where I use @FormParam. – user2144555 Apr 04 '13 at 16:54
  • are you also use 'application/x-www-form-urlencoded' with your client? – Zelldon Apr 04 '13 at 18:37
  • yes, and I can test the PUT methods with the REST console in both ways. But it strange that the error still appears if I don't use @FormParam with multipart/form-data. – user2144555 Apr 04 '13 at 19:51
  • http://stackoverflow.com/questions/8658251/severe-error-for-using-multipart-form-data-for-a-file-upload-service-apache there is the same probleme ß The answer is: You will have to download and use jersey-multipart.jar – Zelldon Apr 04 '13 at 20:00
3

To issue a put-request from a browser you could use jQuery's jQuery.ajax(). (http://api.jquery.com/jQuery.ajax/)

For example:

$.ajax({
  url: "test-url",
  type: "PUT",
  data: {userid: 1}
})

Would send a put-request to test-url with the specified data.

Ayonix
  • 1,872
  • 1
  • 15
  • 14