REST API

What is An API?

· Application Program Interface

·APIs Are Everywhere

·Contract provided by one piece of software to another

·Struct request and response

 

Analogy 1(类比)

API is basically a messenger or a waiter between running software 

 

Analogy 2

 

this as an API its formatted to take certain shapes so the client has to format the

request as a circle square or triangle,this is similiar to how a API works, and you can think the shapes

as the API standard whether it's JSON or sth else.

 

What Is REST?

·Representation State Transfer

·Architecture style for designing networked applications

·Relies on a stateless,client-server protocol, almost always HTTP

·Treats server objects as resources that can be created or destroyed

·Can be used by virtually any programming language

 

HTTP Methods

·GET: Retrive data from a specified resource

·POST:Submit data to be processed to a specified resource

·PUT:Update a specified resource

·DELETE:Delete a specified resource

 

(rarely use)

·HEAD:Same as get but does not return a body

·OPTIONS:Returns the supported HTTP methods

·PATCH:Update partical resources

 

Endpoints

The URI/URL where api/service can be accessed by a client application

 

GET https://mysite.com/api/users

GET https://mysite.com/api/users/1 OR https://mysite.com/api/users/details/1

POST https://mysite.com/api/users

PUT https://mysite.com/api/users/1 OR https://mysite.com/api/users/update/1

DELETE https://mysite.com/api/users/1 OR https://mysite.com/api/users/delete/1

 

Authentication

Some API's require authentication to use their service. This could be free or paid

curl -H "Authorization:token OAUTH-TOKEN" https://api.github.com

curl https://api.github.com/?access_token=OAUTH-TOKEN

curl "https://api.github.com/users/whatever/client_id=xxxx&client_secret=yyyy"

 

Let's Look at a example;

 

https://developer.github.com/v3/

 

github的接口可以接收你发送的get请求,如果次数过多的话会报错。(防止服务器压力,此时需要你进行身份验证,下面实例展示这种情况以及如何进行身份验证)

 

这里使用了POSTMAN 发送了一个GET请求

 

specific user:

过多请求后出现error:

这时需要使用Authentication(身份验证),

进入这个网站,

 

 

点击 Register application,得到了 Client ID 和 Client Secret

 

 查看https://developer.github.com/v3/

看最下方的sent as parameter这种验证方式,

 

现在继续发个带参数的get请求,就会发现不会报错了

 

下一代的API标准可能是GraphQL,

进入 https://developer.github.com/ 查看

 

posted @ 2018-03-16 13:58  hh9515  阅读(187)  评论(0编辑  收藏  举报