Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST services - testing PUT methods in the browser

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

like image 240
user2144555 Avatar asked Apr 04 '13 13:04

user2144555


People also ask

What does put method do in REST API?

A PUT method puts or places a file or resource precisely at a specific URI. In case a file or a resource already exists at that URI, the PUT method replaces that file or resource. If there is no file or resource, PUT creates a new one.

Can we test API in browser?

APIs can be tested in several ways and using different tools: API Explorer - Feature within the Yale Developer Portal to try the Portal APIs. Postman - Powerful browser extension or mac app to test APIs. SOAPUI - Open-source web service testing application.


2 Answers

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'

like image 142
Zelldon Avatar answered Nov 09 '22 13:11

Zelldon


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.

like image 22
Ayonix Avatar answered Nov 09 '22 13:11

Ayonix