best practices for designing web api

restful
why:
- meaningful
This will be improve efficiency , less documents , just read the code - auto generate support
Resource can be achieve automatically without writing any code according to the data model or (java) repository - DRY
It becomes unnecessary to think about what's good web url for providing api to the client developer --- Just following the standard
related resource
example get the portfolios of 90 days of use id 123 :
also another :
use standard http status code
standard without extra documents, we should return http status code according to the standard
useful http status code we should know:
- 200 OK : response for GET PUT DELETE request .
- 201 Created
- 304 Not Modified
- 302 Found : Redirect
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden : logged in user, but not allow to access the resource
- 405 Method Not Allowed: usually for POST/PUT/DELETE request but is now request by GET request
- 429 Too Many Request: for rate limit
401 403 405 are usually used right
when a POST request post succeed, but no new result generated, we should return 200 , instead of 201
security
- with authentication :
Many of the request should first logged in to get the access token in order to have the permission to access resources. Usually we have several simple ways to carry the token in the request body :- within the http header
- within the cookies
- within the url : http://www.example.com/user/123?token=a12dc5
- with hash
Another way to keep the web request safe is to use hash code the encrypt the web parameters , example: http://www.example.com/user/123/order/541?amount=100&product=wade&hash=d328af;
hash the parameters : amount=100&product=wade
besides , usually the parameters is sorted by alphabet
To avoid middle man problem, we should use https.
standard Oauth 2.0 example
prosper oauth flow
simple we should do :
- register to get client id and client secrect
- app granted by the user to get a auth_key according to user's grant permission
- now we can access the api via the access token and refresh token, both of which get an expire time
- if token has expired , we need to re-access the api
request example:
POST <prosper_base_address>/security/oauth/token
Accept: application/json
Content-type: application/x-www-form-urlencoded
grant_type=refresh_token&client_id=<your_client_id>&client_secret=<your_client_secret>&refresh_token=<existing_refresh_token_from_user_token_request>
response example:
{
"access_token": "5098afd7-f216",
"token_type": "bearer",
"refresh_token": "7fcb8a8a-e7dd",
"expires_in": 3599
}
Note that:
error standard
some use direct HTTP STATUS CODE , ref to session: ### use standard http status code
- method 1
http status code 401
{
error: "Invalid API key"
}
- method 2:
should support http status code 、error code 、error msg
example with a 200 OK:
{
"code": -1,
"message": "Something gone wrong",
<!-- "description": "optional print your stack message here" --> //this is optional , usally for error stack message
}
dig into well know company api example
mouseflow
update a new website
PUT /websites/{website-id}
curl -X PUT -u my@email.com:token1234 -d '{"name": "myshop2.com", "recordingRate": 2}' https://api-us.mouseflow.com/websites/{website-id}
simple prosper api
https://developers.prosper.com/docs/investor/accounts-api/
Authorization is in header -- which help us to which one is operating user
Version
There way to represent version:
/api/v1/user
/api/user?version=v1
/api/user with req.header set
version=v1
/api/v1/user is better for Load balancer to reganize
And /api/v1/user is better for rest api rather than /api/user?version=v1 , which will be use as a query:
version=v1
At last /api/v1/user is simpler compare with set to req.header
ref
RESTful API 设计指南
best practices for a pragmatic restful api
API 杂谈
rest api design
best api design
auth api design sample
best practices for designing web api的更多相关文章
- Designing a Secure REST (Web) API without OAuth
原文:http://www.thebuzzmedia.com/designing-a-secure-rest-api-without-oauth-authentication/ Situation Y ...
- Asp.Net Web API 2第十一课——在Web API中使用Dependency Resolver
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文主要来介绍在Asp.N ...
- Web API 简单示例
一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...
- ASP.NET Web API中的依赖注入
什么是依赖注入 依赖,就是一个对象需要的另一个对象,比如说,这是我们通常定义的一个用来处理数据访问的存储,让我们用一个例子来解释,首先,定义一个领域模型如下: namespace Pattern.DI ...
- Web API中使用Dependency Resolver
Web API中使用Dependency Resolver 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyo ...
- ASP.NET Core Web API下事件驱动型架构的实现(三):基于RabbitMQ的事件总线
在上文中,我们讨论了事件处理器中对象生命周期的问题,在进入新的讨论之前,首先让我们总结一下,我们已经实现了哪些内容.下面的类图描述了我们已经实现的组件及其之间的关系,貌似系统已经变得越来越复杂了. 其 ...
- Running Web API using Docker and Kubernetes
Context As companies are continuously seeking ways to become more Agile and embracing DevOps culture ...
- ASP.NET Web API实践系列02,在MVC4下的一个实例, 包含EF Code First,依赖注入, Bootstrap等
本篇体验在MVC4下,实现一个对Book信息的管理,包括增删查等,用到了EF Code First, 使用Unity进行依赖注入,前端使用Bootstrap美化.先上最终效果: →创建一个MVC4项目 ...
- Dependency Injection in ASP.NET Web API 2 Using Unity
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
随机推荐
- HDU 4283 You Are the One ——区间dp
参考了许多大佬 尤其是https://blog.csdn.net/woshi250hua/article/details/7973824这一篇 ,最后我再加一点我的见解. 大意是 给定一个序列,序列 ...
- HDU 6186 CS Course(前缀+后缀)
http://acm.hdu.edu.cn/showproblem.php?pid=6186 题意:给出n个数,共有n次询问,每次询问给出一个数p,求除去第p个数后的n-1个数的&.|.^值. ...
- 补充一下 sizeof
sizeof是一个运算符,给出某个类型或变量在内存中所占据的字节数 sizeof(int) //告诉你int占据几个字节 sizeof(i) //告诉你i这个变量占据几个字节 注:sizeof是静态运 ...
- 《Practical Vim》第八章:利用动作命令在文档中移动
面向单词的移动 定义: Vim 提供了面向单词的动作命令,用于将光标正向/反向移动一个单词; 功能 命令 正向移动到下一单词开头 w 反向移动到上一单词的开头 b 正向移动到下一单词(当前单词)的结尾 ...
- Oracle DB 使用RMAN恢复目录
• 对恢复目录和RMAN 资料档案库控制文件的使用进行比较• 创建和配置恢复目录• 在恢复目录中注册数据库• 同步恢复目录• 使用RMAN 存储脚本• 备份恢复目录• 创建虚拟专用目录 RMAN 资料 ...
- GANs用于文本生成
上学期期末老师给了我本杂志让我好好看看里面的Gans网络是如何应用在文本生成上的,文章里面也没有介绍原理性的东西,只是说了加入这个Gans模型后效果有多好,给出了模型架构图和训练时所用的语料例子,也没 ...
- CSRF理解与防御
一.说明 记得以前去面试技术也不太会但你总得讲点东西,让面试时间长一些让面试官觉得你基础还可以,当时选的就是名头比较大的OWASP TOP 10.TOP 10嘛你总得拿出至少三个点来讲的细一些以证明你 ...
- STL 小白学习(4) deque
#include <iostream> #include <deque> //deque容器 双口 using namespace std; void printDeque(d ...
- quartz.net的使用
Quartz.NET简介 Quartz.NET作业调度框架是伟大组织OpenSymphony开发的quartz scheduler项目的.net延伸移植版本. Quartz.NET是一个开源的作业调度 ...
- angualrjs 文本框去掉表情
html: <textarea ng-module="dataText"></textarea> js: <script> var BQ_RAN ...