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:

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

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

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


  • 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:

  • Each service is different.
  • 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:

  • 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?
  • 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.)?
  • 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)?
  • Have you actually written any code to consume an existing service?
  • 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.


    For your purposes, hitting a restful API will be very similar to interacting with a server via regular AJAX. The difference is that this API will conform to the restful model, where various CRUD-like operations will conform to REST's particular pattern of using specific HTTP verbs [GET, PUT, POST, DELETE] and also its pathing model of referring to an entity.

    Have a look:

    http://www.restapitutorial.com/lessons/httpmethods.html

    If you want to have a play with a restful API, there's a few frameworks out there where you can spin up your in a few minutes.

    I'd probably use Sinatra for this; although there many options:

    http://www.andrewhavens.com/posts/20/beginners-guide-to-creating-a-rest-api/

    Just google "restful api example [your server-side language of choice]; there's a lot of material out there.

    In short order you'll want to explore how cross server side scripting is done with a restful API, and how to use JSONP:

    http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

    链接地址: http://www.djcxy.com/p/12334.html

    上一篇: WCF服务的REST / SOAP端点

    下一篇: JavaScript:学习如何“使用RESTful API”