Creating a REST Request
Tuesday, June 30, 2009
The Yahoo! Search Web Services are all REST services. That means you can easily construct request URLs that will work in your browser, on the command line, and in your code. For our example, we'll construct a query to search the web for PDF files containing the term "finances". See the web search documentation to understand the details of this service.
URL Construction
Most API requests will use GET. If the specific documentation requires POST, see the section on POST URL construction below.
All search request URLs start with the hostname and resemble the following sample:
http://search.yahooapis.com
After the hostname are the service name and version number:
/WebSearchService/V1/
Next is the method followed by a question mark.
webSearch?
Those components form the base URL.
http://search.yahooapis.com/WebSearchService/V1/webSearch?
The method is followed by the actual query parameters, which take the form argument=value, where the arguments and values are url encoded. Multiple parameters are separated by an ampersand (&). The following example searches the web database for PDF files containing the term "finances":
http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=finances&format=pdf
The API documentation for each service provides the service name and lists all of the mandatory and optional parameters, in addition to describing the response structure, rate limits, etc.
POST URL Construction
Using POST to communicate with the API is very similar to using GET. All search request URLs still begin with the hostname, service name, version number and method:
http://search.yahooapis.com/WebSearchService/V1/contextSearch
Note that there is no question mark following the method.
The query parameters are passed in the POST data block. They still take the form argument=value and are properly url encoded. Multiple parameters are separated by an ampersand (&).
appid=YahooDemo&query=madonna&context=Italian+sculptors+and+painters
0 comments: to “ Creating a REST Request ”
Post a Comment