Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Learning how to "consume RESTful APIs"

I've been coding JS for a few years, and am now learning more. Seeing a lot of employers asking for "knowledge of REST APIs" or "experience consuming RESTful services."

I know the basics of AJAX, both in native JS and jQuery. I've done a fair bit 'o goggling on REST, both on SO and the web. There seems to be a ton of info on how to build RESTful APIs server-side with JAVA, C#, etc, but little on how to access those services with JavaScript.

Here are questions:

  1. Is "consuming RESTful services" another way of saying "getting data from a server with AJAX", or something else altogether?

  2. If it is something else, where are some good tutorials on the subject?

  3. Once I get the basics down, where can I find RESTful APIs on the web to consume?

like image 438
joe schmoe Avatar asked Oct 12 '14 23:10

joe schmoe


People also ask

Can you use REST API with JavaScript?

Today, most modern web applications leverage REST architecture to enhance the dynamic capabilities of the website. This growing demand for interaction with REST API using JavaScript has surged the development of new libraries that allows developers to make HTTP requests effectively.

What are the ways to consume REST API in Java?

Just make an http request to the required URL with correct query string, or request body. For example you could use java. net. HttpURLConnection and then consume via connection.

What does it mean to consume a REST API?

Similarly, the act of consuming or using a REST API means to eat it all up. In context, it means to eat it, swallow it, and digest it — leaving any others in the pile exposed.


1 Answers

Is "consuming RESTful services" another way of saying "getting data from a server with AJAX", or something else altogether?

No, not at all, but you certainly could consume your own REST API using AJAX. Ajax typically just means xmlHttpRequest, which is inherently a web browser concept. A RESTful API is more like a service interface. In many ways, it's a web service, but without the complexity of SOAP.

Essentially, REST is a way of expressing a transaction or query over HTTP by using verbs and nouns. For example, to get information, you use... wait for it... GET! To put information, you use PUT (or POST, the tutorials below will explain the difference). And to delete something, you use DELETE. It's rather neat. It almost reads like a sentence over HTTP:

GET /users/123
DELETE /users/123

You can guess what those would do.

Because these are just HTTP requests, they can often be consumed using AJAX, however, that is not necessary. In fact, I find REST API's more useful for exposing services or data to other applications, again, just like a web service.

If it is something else, where are some good tutorials on the subject?

It's hard to give a good tutorial on consuming RESTful services because of two reasons:

  1. Each service is different.
  2. It depends on what language / platform you are working in.

Bret's answer mentioned a few good tutorials.

Here is a SO answer which shows a way to consume REST directly in Java: How to consume REST in Java

Here is a reasonable tutorial on how to create a RESTful API using the JAX-RS API in Java and then shows how to consume it: http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/

I've also had good success with Jersey's REST client. Their REST (JAX-RS) server software is a royal pain (in my opinion), but their client is pretty slick.

I wrote a simple proof-of-concept todo list REST API last spring in Java and the integration tests show good use of the Jersey client to consume the API for testing purposes.

https://github.com/brandonramirez/todo-api/blob/master/src/test/java/com/brandonsramirez/todoApi/TaskResourceTest.java

I'm also a fan of the Unirest client, which is a REST client translated into several languages with a similar interface. They have an Objective-C version, a Java version, a JavaScript / node.js version, a .NET version, etc.

If an employer is looking for experience with consuming REST API's, they probably want five things:

  1. Do you understand the basic semantics of exchanging data over HTTP using the various methods and the design patterns, naming conventions, etc. that go with it along with it?
  2. Do you recognize the de facto standards for mapping CRUD operations onto HTTP requests (like using GET /resources to list all resources, POST /resources to create a new resource, PUT /resources/x to update resource X, etc.)?
  3. Are you familiar with the various HTTP response codes and when/how they would be used? Like can you always assume that a successful response yields a 200 status code? If you create a new resource, is there another commonly used status code (yes, there is)?
  4. Have you actually written any code to consume an existing service?
  5. Can you test a service easily? For example, using curl to create issue an HTTP request very precisely.

And a bonus, but most people don't have a good grasp of this (I struggle to understand its usefulness personally), is hyper-media for versioning by twiddling with the Accepts header.

Once I get the basics down, where can I find RESTful APIs on the web to consume?

All over! Everybody has one! Twitter, Facebook, Twilio, Stripe, Google, Edmodo, Amazon. I could go on forever. In the IaaS world, Amazon Web Services and Rackspace Cloud both offer RESTful API's for managing your cloud infrastructure. For example, I can make an HTTP POST request to a URL that represents my set of servers, and boom, a new server (virtual machine) is provisioned to me.

https://stripe.com/docs/api

https://api.imgur.com/

There are so many that there are even hub services to help consumers find API's and providers to publish them, such as Mashape and Mashery, though the latter appears to be more focused on providers than consumers, judging from their web site.

The github project I posted earlier, with my proof-of-concept, accesses a REST API from Searchly and Twilio so that I can search for tasks, and when one is marked complete, send me a text message, again, all from HTTP requests. However, I used the client libraries from those providers rather than using direct HTTP libraries just to make the code leaner and more focused, without worrying about all of the HTTP plumbing.

like image 115
Brandon Avatar answered Nov 13 '22 09:11

Brandon