Creating a REST webservice reference

A Representational State Transfer webservice or REST service is another kind of webservice standard. REST does not necessarily use WSDL or SOAP messaging, but can be called directly by using methods like POST and GET and other HTTP operations. So we can call a REST service directly from a URL unlike SOAP. This makes testing a lot easier.

APEX developers can use REST webservices and the wizard interface offers many possibilities for creating the call.

In this recipe, we are going to use the opportunities that APEX offers us to create a page based on a public REST webservice—the popular photo application Flickr.

Getting ready

Before starting this tutorial, create a Flickr account at http://www.flickr.com and request an API key.

How to do it...

  1. Go to Shared Components and click on Web Service References.
  2. Click the Create button.
  3. Select the radio button next to REST and click Next.
  4. In the Name field enter Flickr REST service.
  5. In the URL field enter http://www.flickr.com/services/rest/.
  6. Leave the rest on default and click Next.

In the Input parameters, we are going to enter three parameters.

  1. Name the first method and click Add Parameter.
  2. Name the second api_key and click Add Parameter.
  3. Name the last user_id and click Next.
  4. Enter for XPath to Output Parameters the string /rsp.
  5. Click Create to save the webservice call.

Now we are ready to test if everything is OK.

  1. Navigate back to the list of Web Service References.
  2. Make sure the Report View is selected by pressing the corresponding button in the toolbar above the list.
    How to do it...
  3. Now click the Test button for our Flickr REST Service.
  4. To make this work enter values for the three Input Parameters we created earlier:
    • Method = flickr.people.getPublicPhotos
    • Api_key = your Flickr API Key
    • User_id = your Flickr user id (or use mine: 52012402@N05)
  5. Now click the Test button.

After a little wait a Response is returned as an XML file, containing all publicly available pictures for the selected user.

How to do it...

How it works…

There are a lot of other differences between REST and SOAP. Both of them have advantages. Some of these for REST are:

  • It is lightweight without a lot of extra XML markup
  • It has human readable results
  • It's easy to build without any toolkits

The main advantages of SOAP are:

  • Easier to consume (most of the time).
  • Contract driven.
  • There are a lot of development tools available.

The industry lately seems to be in favor of using REST for internet services. A lot of the larger companies (such as Twitter, Google, and Amazon) have chosen this standard for their webservices. SOAP still seems to be the standard for enterprise applications. But choosing REST or SOAP in a project is completely dependant on the specifications of that project.

There's more...

With the information from this recipe, it is possible to create a page to enter the required parameters and to display the response in a more readable format. We will do so in the next recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.107.25