REST stands for Representation State Transfer. REST is an architecture style that allows us to build lightweight and scalable web services. These services are called RESTful web services. Without getting to far into the weeds, these services allow us to send a request from our web application which will, in turn, send our application a response. For example, say you are ordering something on a web site and are being asked to choose a shipping method for the item. Now, for each shipping method the site is showing you what the price is for that method. How do they know what UPS, or any other carrier, will charge for this item. Well, the carrier has an API that allows the web application access to certain data to tell you, the user, what the cost will be for that method. And this is accomplished by the web application making a request to the API, receiving a response, and then parsing that response. Now, that we have a very brief overview of REST, let’s start by looking how this is used in action.
First, we get our user inputs when the button is clicked. The next few lines might look a little new. Here we are using the Yahoo API, a RESTful web service, that allows us to write a query in the Yahoo Query Language (YQL). Here, we are basically using an intermediary that will perform the search on google.books. We are using this method, because if we interact directly with the Google Books API we would need to get an API key. So, while that would be necessary for a production application, for now we will use the Yahoo API for brevity. The yqlBaseURL allows us to access the Yahoo API to use YQL. You will notice that for the yqlQuery, we use string concatentation to insert our user inputs into the query. Later we will tie these both together to send our request.
So, now you have the basics of how to send and receive a response to a RESTful web service. With that, we unlock the true power of the web, and how we can send and receive data. Next week, we will dive more into the responses we got back from the server and how we can parse the data to make it useful. Until next week, go ahead and play around with JSON.parse, and see if you can figure out how to parse the JSON from the response. Remember to comment below if you have any questions, and share this post on your favorite social media platform.