Презентация - HTTP, REST and Postman

HTTP, REST Services, Postman, Exploring the REST APIs with Postman - JS Applications - ноември 2016

 

Материали от темата

Съдържание на документа

HTTP and REST

HTTP, RESTful Web Services, HTTP and REST Tools: Postman, Fiddler

SoftUni Team

Technical Trainers

Software University

http://softuni.bg

HTTP
and REST


Table of Contents

The HTTP Protocol

HTTP Developer Tools

REST and RESTful Services

Accessing the GitHub API

Using Firebase BaaS

Using Kinvey mBaaS

2


3



sli.do
#http-rest

Have a Question?


4

HTTP (Hyper Text Transfer Protocol) 	

Text-based client-server protocol for the Internet

For transferring Web resources (HTML files, images, styles, etc.)

Request-response based



HTTP Basics





HTTP request

HTTP response

Web Server

Web Client


5

HTTP defines methods to indicate the desired action to be performed on the identified resource

HTTP Request Methods

Method

Description

GET

Retrieve / load a resource

POST

Create / store a resource

PUT

Update a resource

DELETE

Delete (remove) a resource

PATCH

Update resource partially

HEAD

Retrieve the resource's headers


GET /users/testnakov/repos HTTP/1.1

Host: api.github.com

Accept: */*

Accept-Language: en

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36

Connection: Keep-Alive

Cache-Control: no-cache

<CRLF>

6

HTTP GET Request – Example

HTTP headers

HTTP request line

The request body is empty


7

HTTP POST Request – Example

POST /repos/testnakov/test-nakov-repo/issues HTTP/1.1

Host: api.github.com

Accept: */*

Accept-Language: en

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible;MSIE 6.0; Windows NT 5.0)

Connection: Keep-Alive

Cache-Control: no-cache

<CRLF>

{"title":"Found a bug",

 "body":"I'm having a problem with this.",

 "labels":["bug","minor"]}

<CRLF>

HTTP request line

HTTP headers

The request body holds  the submitted data


8

HTTP Response – Example

HTTP/1.1 200 OK

Date: Fri, 11 Nov 2016 16:09:18 GMT+2

Server: Apache/2.2.14 (Linux)

Accept-Ranges: bytes

Content-Length: 84

Content-Type: text/html

<CRLF>

<html>

  <head><title>Test</title></head>

  <body>Test HTML page.</body>

</html>

HTTP response status line

HTTP response headers

HTTP response body


9

HTTP Response Status Codes

Status Code

Action

Description

200

OK

Successfully retrieved resource

201

Created

A new resource was created

204

No Content

Request has nothing to return

301 / 302

Moved

Moved to another location (redirect)

400

Bad Request

Invalid request / syntax error

401 / 403

Unauthorized

Authentication failed / Access denied

404

Not Found

Invalid resource 

409

Conflict

Conflict was detected, e.g. duplicated email

500 / 503

Server Error

Internal server error / Service unavailable


10

The Content-Type / Content-Disposition headers specify how the HTTP request / response body should be processed

Examples:

Content-Type and Disposition

Content-Type: application/json

Content-Type: application/pdf

Content-Disposition: attachment; filename="Financial-Report-April-2016.pdf"

UTF-8 encoded HTML page. Will be shown in the browser

This will download a PDF file named Financial-Report-April-2016.pdf

Content-Type: text/html; charset=utf-8

JSON-encoded data


HTTP Developer Tools

Chrome Dev Tools, Fiddler, Postman


12

Chrome Developer Tools

Chrome Developer Tools


13

Fiddler

Fiddler


14

HTTP Tools for Developers

Postman REST Client


15

Representational State Transfer (REST)

Architecture for client-server communication over HTTP

Resources have URI (address)

Can be created / retrieved /
modified / deleted / etc.

RESTful API / RESTful Service

Provides access to server-side
resources via HTTP and REST

REST and RESTful Services


16

Create a new post

POST http://some-service.org/api/posts

Get all posts / specific post

GET http://some-service.org/api/posts 

GET http://some-service.org/api/posts/17

Delete existing post

DELETE http://some-service.org/api/posts/17

Replace / modify existing post

PUT / PATCH http://some-service.org/api/posts/17

REST and RESTful Services – Example


GitHub REST API

Accessing GitHub through HTTP


18

List user's all public repositories:



Get all commits from a public repository:





Get all issues / issue #1 from a public repository

GitHub API

GET

https://api.github.com/users/testnakov/repos

GET

https://api.github.com/repos/testnakov/softuniada-2016/commits

GET

/repos/testnakov/test-nakov-repo/issues

/repos/testnakov/test-nakov-repo/issues/1 

Check your solution here: https://judge.softuni.bg/Contests/356


19

Get all labels for certain issue from a public repository:





Create a new issue to certain repository (with authentication)

GitHub API (2)

GET

https://api.github.com/repos/testnakov/test-nakov-repo/issues/1/labels

POST

https://api.github.com/repos/testnakov/test-nakov-repo/issues

Authorization: Basic base64(user:pass)

Headers

{"title":"Found a bug",

 "body": "I'm having a problem with this."}

Body

Check your solution here: https://judge.softuni.bg/Contests/356


20

Submit the result of your request (the returned HTTP response)

Submitting Problems to the Judge


Firebase

Real-Time Cloud DB and App Platform by Google


22

Firebase is a cloud-based DB, storage and app platform (BaaS)

REST API, JSON-based document DB, free and paid plans

Register at https://console.firebase.google.com

Create a new project and
play with it

Firebase


23

Firebase – Put Some Data in the DB



This is your DB URL

The data is a hierarchical key-value structure (tree)


24

Enable unauthorized access to your DB

For educational purposes only!

This is a security hole, don't do it in real apps

Access your data through the REST API

Firebase REST API

GET

https://testapp-fc138.firebaseio.com/.json


25

Accessing Firebase REST API with Postman

Append .json to your DB object URL


26

Firebase REST API – CRUD Operations

GET

https://testapp-fc138.firebaseio.com/.json

GET

https://testapp-fc138.firebaseio.com/books.json

GET

https://testapp-fc138.firebaseio.com/books/1.json

GET

https://testapp-fc138.firebaseio.com/books/1/author.json

POST

https://testapp-fc138.firebaseio.com/books.json

{"title":"New title", "author":"New author"}

Body

DELETE

https://testapp-fc138.firebaseio.com/books/6.json

Check your solution here: https://judge.softuni.bg/Contests/356


27

Firebase REST API – CRUD Operations (2)

PUT

https://testapp-fc138.firebaseio.com/books/7.json

{"title":"Edited title", year:1980, ISBN:"954X"}

Body

PATCH

https://testapp-fc138.firebaseio.com/books/7.json

{"year":1981, "author":"Author Changed"}

Body

PUT

https://testapp-fc138.firebaseio.com/books/7/author.json

"New author was assigned"

Body

DELETE

https://testapp-fc138.firebaseio.com/books/7/author.json

Check your solution here: https://judge.softuni.bg/Contests/356


Kinvey

Collection-Based Cloud DB (mBaaS)


29

Kinvey is a Mobile Back-End as a Service (mBaaS)

Holds your app / mobile app data in the cloud

Anyone can register and create an app

Kinvey apps hold users and user data

Users (API for creating an account)

Data collections (API for CRUD operations)

Files (upload / download / delete)

Kinvey as JS Back-End


30

Create a developer account in Kinvey

https://console.kinvey.com/sign-up 

Kinvey: Registration


31

Create an app backend (e.g. MyTestApp)

https://console.kinvey.com/apps/new

You get App ID + App secret keys (account)

Kinvey: Create an App Backend








32

Create a user (e.g. guest / guest)

Kinvey: Create a User


33

Create a
data collection
(e.g. posts)

Kinvey: Create a Data Collection


34

Create some data columns for the posts, e.g. title and body

Kinvey: Create Data Columns


35

Create some data rows for the posts collection



Kinvey: Create Data Rows


36

Test Your Backend with Postman

Basic authentication
user: guest

pass: guest

Check your solution here: https://judge.softuni.bg/Contests/356



https://baas.kinvey.com/appdata/{app_id}






37

URL: https://baas.kinvey.com/appdata/{app_id}/posts  

Method: GET

Authentication: Basic

User: guest

Pass: guest

Kinvey and Postman: List All Posts

Check your solution here: https://judge.softuni.bg/Contests/356


38

URL: https://baas.kinvey.com/appdata/{app_id}/posts  

Method: POST

Authentication: Basic

User / pass: guest / guest

Request body

title: New Title

body: New Post Body

Kinvey and Postman: Create a New Post

Check your solution here: https://judge.softuni.bg/Contests/356


39

URL: https://baas.kinvey.com/appdata/{app_id}/posts/id 

Choose an existing post's ID

Method: DELETE

Authentication:
Basic

User / pass:
guest / guest

Body: (empty)

Kinvey and Postman: Delete an Existing Post

Check your solution here: https://judge.softuni.bg/Contests/356


40

URL: https://baas.kinvey.com/appdata/{app_id}/posts/id 

Choose an existing post's ID

Method: PUT

Authentication: Basic

User / pass: guest / guest

Body (JSON):

Kinvey and Postman: Edit an Existing Post

Check your solution here: https://judge.softuni.bg/Contests/356

{"title":"edited title", "body":"edited author", "hidden":true}


41

URL: https://baas.kinvey.com/user/{app_id}/login

Method: POST

Authentication: Basic

User / pass: app_id : app_secret

Body:



Returns: authtoken, e.g.

Kinvey and Postman: Login

Check your solution here: https://judge.softuni.bg/Contests/356

"authtoken": "fd6d989d-0930-4c…wI="

{"username":"…", {"password":"…"}


42

URL: https://baas.kinvey.com/user/{app_id}/_logout

Method: POST

Authorization:

Kinvey <authtoken>

Use the token given
by the login request

Returns:

 204 No Content

Kinvey and Postman: Logout



Authorization: Kinvey <authtoken>


Practice: Accessing the REST API from GitHub, Firebase, Kinvey

Live Exercises in Class (Lab)


44

HTTP is text-based request-response protocol

REST uses GET, POST, PUT, PATCH, DELETE

RESTful services address resources by URL

Provide CRUD operations over HTTP

GitHub API provides access to users, organizations, repos, commits, issues, wikis, gist, pull requests, …

Firebase is JSON-based cloud database (mBaaS) with REST API

Kinvey is collection-based could database (mBaaS)

Summary


https://softuni.bg/courses/javascript-applications

HTTP and REST


License

This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International" license

46


Free Trainings @ Software University

Software University Foundation – softuni.org

Software University – High-Quality Education, Profession and Job for Software Developers

softuni.bg 

	Software University @ Facebook

	facebook.com/SoftwareUniversity

	Software University @ YouTube

	youtube.com/SoftwareUniversity

	Software University Forums – forum.softuni.bg