You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: js/lesson4/tutorial.md
+23-33Lines changed: 23 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ title: HTTP Requests, AJAX and APIs
7
7
8
8
In this tutorial we are going to look at:
9
9
10
-
*The HTTP protocol
10
+
* HTTP Requests
11
11
* APIs
12
12
* AJAX
13
13
* JSON
@@ -18,36 +18,28 @@ In this tutorial we are going to look at:
18
18
19
19
By the end of this tutorial you will have built:
20
20
21
-
* A webpage that can retrieve information about a specified GitHub user
22
-
*A webpage that can show the upcoming schedule for BBC shows
21
+
* A webpage that can retrieve information from a GitHub user
22
+
*Sent yourself an SMS using the Vonage API
23
23
24
-
# HTTP Requests
24
+
##HTTP Requests
25
25
26
-
## What are HTTP Requests?
26
+
###What are HTTP Requests?
27
27
28
-
Every time the browser fetches data from a server (which could be a page, an image, a script etc) it does it using HTTP. HTTP is the **H**​yper<strong>T</strong>ext **T**​ransport **P**​rotocol. The server then sends back a **response**. An API is an easy way of fetching information from a remote service, in a way that's easy for a computer to understand.
GitHub offers a [simple API](https://www.githubstatus.com/api/) for viewing its current and historical server availability.
30
+
Every time the browser fetches data from a server (which could be a page, an image, a script etc) it does it using HTTP. The server then sends back a **response**. An API is an easy way of fetching information from a remote service, in a way that a computer can understand, typically in **JSON** format (we'll talk more about JSON later).
31
31
32
-
>Availability means whether or not the GitHub website was accessible to users and accepting traffic. If your website is down, it is not available.
32
+
GitHub offers a [simple API](https://www.githubstatus.com/api/) for viewing its current and historical server availability. Availability means whether or not the GitHub website was accessible to users and accepting traffic. If your website is down, it is not available.
33
33
34
-
You can access an API in your web browser. Just pop the following into the address bar:
34
+
You can access an API in your web browser. Pop the following into the address bar:
35
35
36
36
https://www.githubstatus.com/api/v2/summary.json
37
37
38
-
If you are on a mac or a linux/unix machine, you can access the API using curl:
> Paste the following command into Terminal, which you can find in Finder - first go into the Applications folder, then Utilities.
43
-
44
-
Here is an example of the **GET** requests issued by the [wishlist tutorial](https://tutorials.codebar.io/examples/wishlist/index.html).
45
-
46
-
\*You can view any requests issued by a website by going to the Network (or Net) tab.
38
+
You can view all requests issued by a website by going to the Network (or Net) tab.
47
39
48
40

49
41
50
-
As part of the response, a request gives back a **status code**. You can use this to identify if the request was successful or not.
42
+
As part of the response, a request gives back a **status code**. You can use this to identify if the request was successful or not. Below is a list of the most common.
51
43
52
44
| Status code | Message | Description |
53
45
| ------------| ------- | ------------ |
@@ -56,23 +48,17 @@ As part of the response, a request gives back a **status code**. You can use thi
56
48
| 400 | Bad Request | The server did no understand the request |
57
49
| 404 | Not Found | The server could not find the requested resource |
58
50
59
-
### HTTP Verbs
51
+
### HTTP Methods
60
52
61
-
HTTP verbs are sent by the browser or client, and along with the URL used and data transmitted form part of the instruction to the API. There are several verbs, but in this tutorial we will be primarily using GET. GET is used to fetch information from an API. Another common verb is POST, which is used to create a new object on the remote service.
53
+
There are several HTTP Methods, but in this tutorial we will primarily be using **GET** and **POST**. GET is used to request data from your specified API, whereas POSTis used to send data to your specified API.
62
54
63
55
## Exercise 1 - Retrieve GitHub user information
64
56
65
-
We'll build a small application that gives us back information about a GitHub user - we want to show their username, information and their picture. [Download](https://gist.github.com/deniseyu/d1bc03b8091153b4b1a7/download) the exercise files or clone them directly from Github `https://gist.github.com/deniseyu/d1bc03b8091153b4b1a7`
66
-
67
-
GitHub offers an API where you can request information for a given username. The verb to use is GET, and the url is `https://api.github.com/users/<username>`. For codebar, this would be: `https://api.github.com/users/codebar`. Again, to request this you can use curl:
57
+
We are going to build a small application that requests information about a GitHub user - we want to show their username, information and picture.
68
58
69
-
$ curl -XGET https://api.github.com/users/codebar
59
+
> Take a second to think about what HTTP Method we will use to retrieve this data. Is it GET or POST?
70
60
71
-
or, as GET is the default verb, just:
72
-
73
-
$ curl https://api.github.com/users/codebar
74
-
75
-
Again, you can simply access this URL in your web browser by inserting `https://api.github.com/users/codebar` into the address bar.
61
+
If you guessed GET congrats! Now let's retrieve some data. See what happens when you put `https://api.github.com/users/codebar` into your address bar.
76
62
77
63
The response will look something like the JSON data below, which we have shortened:
78
64
@@ -96,8 +82,12 @@ The response will look something like the JSON data below, which we have shorten
96
82
97
83
This data is what's called key value pairs, meaning that the name of the field is displayed immediately before the value. As you can see, the URL for the avatar (user's icon) is in the `avatar_url` field, and is `https://avatars.githubusercontent.com/u/9906?v=2`.
98
84
85
+
> Run the same URL but with your GitHub name.
86
+
99
87
### Getting started
100
88
89
+
Now [Download](https://gist.github.com/deniseyu/d1bc03b8091153b4b1a7/download) the exercise files or clone them directly from Github `https://gist.github.com/deniseyu/d1bc03b8091153b4b1a7`.
90
+
101
91
First, open the HTML page supplied in the download. As you can see, there is a box to type in a username. When the user has typed in the username, they should be able to trigger the API call to GitHub by pressing \<enter\>.
102
92
103
93
The following code allows you to listen for a keypress on the input field, and to see if it was the \<enter\> key that was pressed.
@@ -147,13 +137,13 @@ function getGithubInfo(username) {
147
137
148
138
We create an `XMLHttpRequest` object and then call the `open` method, passing three arguments to the GitHub API.
149
139
150
-
1. the `verb` - in this case, `'GET'`
140
+
1. the `method` - in this case, `'GET'`
151
141
2. the `url` - in this case the url eg https://api.github.com/users/codebar
152
142
3. whether or not to run this request synchronously or asynchronously.
153
143
154
144
In this case, we'll specify synchronously by passing `false`. This means the browser will wait for the call to the GitHub API to finish before continuing.
155
145
156
-
> Making requests synchronously is not good practice, but we're doing it for now to keep things simple. Your browser may show a deprecation warning but the request will still work. We'll move onto asynchronous requests further down once we have the basics of APIs covered.
146
+
Making requests synchronously is not good practice, but we're doing it for now to keep things simple. Your browser may show a deprecation warning but the request will still work. We'll move onto asynchronous requests further down once we have the basics of APIs covered.
157
147
158
148
You can now call `getGithubInfo`, passing the username, from the `keypress` block above. That will log the data to the console. Next, we need to pass this back to the web page via the DOM.
159
149
@@ -234,7 +224,7 @@ Well done, you've finished! For a bonus, switch your `getGithubInfo` method to r
234
224
235
225
In the following Vonage API tutorial you will create an account, and send yourself an SMS. Just a heads up, it will involve you creating an account and giving your phone number.
236
226
237
-
When signing up select NodeJS as you're language. NodeJS is a runtine enviroment that allows you to run JavaScript on the server than in the browser which is what you would've been doing if you've followed our first 2 tutorials.
227
+
When signing up select NodeJS as your language. NodeJS is a runtine enviroment that allows you to run JavaScript on the server than in the browser which is what you would've been doing if you've followed our first 2 tutorials.
238
228
239
229
(Send yourself an SMS with Vonage)[https://developer.nexmo.com/?utm_source=codebar&utm_campaign=Events&utm_medium=bitly&utm_source=Events]
0 commit comments