Skip to content

siucacm/RESTWorkshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to RESTful services

Welcome, we will be talking about RESTful web services and HTTP protocol.

In the terminal you can go ahead and clone down the repository.

git clone https://github.com/siucacm/WorkshopFour

Remember

Use ls to list the files in the current directory.

Use cd directory_name to change your directory.

What's coming up?

GET http://api.icndb.com/jokes/random

The joke outputted is due to many factors. One of which is Representational state transfer.


REST

REST stands for Representational State Transfer. It is an architectural style to be used for creating web services. With a RESTful web service, you can exchange information with a computer system on the internet.

REST was designed by Roy Fielding in his PhD dissertation. He designed it with the following architectural style constraints:

  • Client-Server architectural style
  • Stateless
  • Cacheable
  • Layered system
  • Uniform interface
  • Code on demand (optional)

HTTP

HTTP stands for Hypertext Transfer Protocol, it is the base of all data communication for internet. We will be using it to help communicate with RESTful services.

HTTP has many request methods to define the action that will be performed. Four of them are the most important. Important methods:

  • GET : Request a resource
  • HEAD : Request a resource but no body
  • PUT/POST : Adding a resource
  • DELETE : Delete a resource

There are some arguments between PUT and POST about which one is used for what :)

Each HTTP request comes with a status code, we are going to focus on 2 of them:

  1. 200 = Okay
  2. 404 = Not found

Anything in the 400s is client side error Anything in the 500s is server side error


GET http://stackoverflow.com/

Now check a summary

HEAD http://stackoverflow.com/

Try any random website and see what you get.

Websites are good use of the HTTP protocol, but an application program interface (API) is way more useful.

API

API : Set of protocols,tools for building software. Java API (Sounds familiar?)

//The below uses javas api
System.out.println("Hello World");

RESTful API's

Fetch resources using HTTP

Type in browser: http://quotes.rest/qod.json?category=inspire

Keep on fully refreshing the page. After a while, you will get what is called a rate limit. This prevents people for misusing restful services.

Lets have a look at the API docs here theysaidso.com

Let's make a rest call

GET https://api.ipify.org?format=json

You just made GET requests to ipify's API requesting a resource. You may have noticed all of this objects we get are wrapped in braces etc

ITS JSON :)


JSON

JavaScript Object Notation is a data-interchange format. Its like a dictionary for those familiar with python. Learn more at JSON.org


Chuck Norris Joke API

To get an understanding on how REST works, we will be looking into the Chuck Norris Joke RESTful API. We can take a look at the API here: https://api.chucknorris.io/.

Let's do the first command listed on the site:

GET https://api.chucknorris.io/jokes/random

We see that a JSON object is outputted.

Testing in front end web development

The below html script gets a joke from the chuck norris api:

<!DOCTYPE html>
<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	</head>
	<body>
		<button type="submit" onclick="getchucknorrisjoke()" >Chuck Norris Joke</button>
		<p id="joke"></p>
		<script>
			function getchucknorrisjoke(){
				var getJoke = $.ajax({
					type: 'GET',
					url: 'https://api.chucknorris.io/jokes/random?category=food',
					dataType: 'json',
					timeout: '3000'
				});
				
				getJoke.done(function(response){
					$('#joke').html(response.value)
				});
			}
		</script>
	</body>
</html>

Check out other public apis


Authentication

GET http://api.wordnik.com/v4/words.json/wordOfTheDay

Sometimes, you need an API KEY to make requests

Never leave your API KEY hanging about in a publically accessible domain. Its dangerous :)

Instead,export it as an environment variable or throw it in a config file.

export KEY="secretkey"

echo $KEY

Sometimes, it can be hard to read a gigantic response like here:

GET http://api.wordnik.com/v4/words.json

We have some tools for that

#Python ships with a tool to pretty print json object
#The line is a pipe which pipes output of command 1 to input of command 2
GET http://api.wordnik.com/v4/words.json | python -m json.tool

Notice how its still a large object. Good guy UNIX ships with some tools that can be handy:

#Go through file slowly
GET http://api.wordnik.com/v4/words.json | python -m json.tool | less 

#head and tail are your friends

Lets get even more hands on

Lets POST some data! Firebase (Backend as a service) allows us to have a database which can be modified through HTTP.

GET https://rest-1663a.firebaseio.com/.json | python -m json.tool

###POST https://rest-1663a.firebaseio.com/.json
### Now enter your object as {"NAME":"LANGUAGE PROFICIENT IN}
### Press CTRL D and give it a couple of seconds.
### Something should pop up in your screen

GET https://rest-1663a.firebaseio.com/.json

#You should see your data

You can modify authentication to change options on who can get and post data.

A small practical use

java -jar APIGetReq.jar
#Type in http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en

Want to learn more? Have a look at the source code and tweak it to display any json object.

Here is a list of a bunch of public apis you can use in your projects