当前位置: 首页 > 编程日记 > 正文

rest api_REST API

rest api

历史 (History)

REST stands for Representational State Transfer protocol. Roy Fielding defined REST in his PhD dissertation in 2000.

REST代表表象小号泰特贸易交接协议。 Roy Fielding在2000年的博士学位论文中定义了REST。

什么是REST API? (What is a REST API?)

REST was developed to provide a uniform interface for

REST的开发旨在为

  • Identifying resources

    识别资源
  • Manipulation of resources

    资源操纵
  • Self descriptive messages

    自我描述性信息
  • Using Hypermedia as the Engine of Application State (HATEOS)

    使用超媒体作为应用程序状态(HATEOS)的引擎

最佳实践 (Best Practices)

基本 (Basics)

Method || http://api.co/v2/cars || http://api.co/v2/cars/1234

方法 || http://api.co/v2/cars || http://api.co/v2/cars/1234

  • GET || List all the cars ||   Retrieve an individual car

    GET || 列出所有汽车|| 取回一辆车
  • POST || Create a new car     || Error

    POST || 创建新车|| 错误
  • PUT || Replace cars collections  ||  Replace the car with id 1234

    放入|| 替换汽车收藏|| 更换ID为1234的汽车

    with new one

    与新的

  • DELETE ||       Delete all cars ||   Delete car with id 1234

    删除|| 删除所有汽车|| 删除ID为1234的汽车

Note while PUT operations either client or server can generate id’s

请注意,无论是客户端还是服务器,PUT操作都可以生成ID

名词好,动词不好 (Nouns are good Verbs are bad)

  • Use nouns to refer resources like cars, fruits etc.

    使用名词来指代诸如carsfruits等资源。

  • Use verbs for action declarations convertMilesToKms, getNutritionalValues

    使用动词进行动作声明convertMilesToKmsgetNutritionalValues

单数还是复数? (Singular or Plural?)

Use correct grammar for declaration

使用正确的语法进行声明

Avoid /person/145

避免 /person/145

Prefer /people/154 Assume to return 154th person from list of people

首选 /people/154假定从人列表中返回第154人

用例 (Use cases)

Use anyone of the below patterns and be consistent!

请使用以下任何一种模式并保持一致!

Case StylesExampleUpperCamelCasehttp://api.fintech.cp/DailyTransactions/TodaylowerCamelCasehttp://api.fintech.cp/dailyTransactions/todaysnake_casehttp://api.fintech.cp/daily_transactions/today

案例StylesExample UpperCamelCase http://api.fintech.cp/DailyTransactions/Today lowerCamelCase http://api.fintech.cp/dailyTransactions/today snake_case http://api.fintech.cp/daily_transactions/today

关系和资源 (Relationships and Resources)

  • Resources can have one-to-many, many-to-many, many-to-one relationships etc. Mapping them correctly is crucial.

    资源可以具有one-to-manymany-to-manymany-to-one关系等。正确地映射它们至关重要。

One-to-Many Mapping

一对多映射

For example, Tickets/145/messages/4 suggests one-to-many relationship between tickets and messages. Meaning 1 ticket has N messages. Message isn’t standalone resource. You can’t have /messages/4.

例如, Tickets/145/messages/4建议ticketsmessages之间的一对多关系。 含义1张票有N消息。 消息不是独立资源。 您不能有/messages/4

Many to Many Mapping

多对多映射

For example, /usergroups/345/users/56 suggests select 345th user group and get user with id 56. However, one user might be in multiple usergroups i.e. /usergroups/209/users/56 is also valid. In such case so for seperating the depedant resource users into a seperate endpoint like /users/56 and provide resource linking in /usergroups/209/users/56

例如, /usergroups/345/users/56建议选择第345个用户组并获取ID为56的用户。但是,一个用户可能在多个usergroups/usergroups/209/users/56也有效。 在这种情况下,为了将消耗资源的users分离到一个单独的端点(如/users/56并在/usergroups/209/users/56提供资源链接

API参数 (API Parameters)

  • PATH : required to access the resource E.g. /cars, /fruits

    PATH: 需要访问资源如/cars/fruits

  • Query Parameters : optional filter the list E.g. /cars?type=SUV&year=2010

    查询参数可选过滤列表,例如/cars?type=SUV&year=2010

  • Body : Resource specific logic. Advance search query. Sometimes it might have both Query and body.

    正文 :特定于资源的逻辑。 提前搜索查询。 有时它可能同时具有查询和正文。

  • Header : Should contain global or platform wide data. E.g. API key parameters, encrypted keys for auth, device type information e.g. mobile or desktop or endpoint, device data type e.g. xml or json. Use header to communicate these parameters

    标头 :应包含全局或平台范围的数据。 例如API密钥参数,用于auth的加密密钥,设备类型信息(例如移动或桌面或端点),设备数据类型(例如xml或json)。 使用标头传达这些参数

HTTP状态码 (HTTP Status Codes)

Use correct status codes

使用正确的状态码

CodesMeaning1xxRequest received and understood.2xxAction requested by client was received, understood and requested.3xxClient must take additional action to complete the request. Most of these status codes are used in URL Redirection.4xxIntended for situations where it seems the error was caused by the client.5xxThe server failed to fulfil a request.

CodesMeaning1xxRequest已接收并被理解。2xx客户端所请求的操作已被接收,理解并被请求。3xxClient必须采取其他操作来完成请求。 这些状态代码大多数用于URL重定向。4xx用于似乎错误是由客户端引起的情况。5xx服务器无法满足请求。

Little more on 2xx!

2xx上的更多内容!

  • 201 Resource Created. POST /cars should return HTTP 201 created with location header stating how to access the resource E.g. location : api.com/cars/124 in header

    201资源已创建。 POST /cars应该返回与创建HTTP 201 location头,说明如何访问资源如locationapi.com/cars/124在头

202 - Accepted

202-已接受

Use this if the task is huge to run. Tell the client, it has accepted the request and will/is process/processing No payload is returned

如果任务要运行,请使用此选项。 告诉客户,它已经接受了请求并且将/正在处理/正在处理没有返回有效载荷

204 - No content

204-无内容

Used when deleted DELETE cars/124 Returns no content. But can also return 200 OK if API intends to send the deleted resource for further processing.

删除后使用DELETE cars/124返回任何内容。 但是,如果API打算发送已删除的资源进行进一步处理,也可以返回200 OK

The dangerous 5xx resources!

危险的5xx资源!

  • 500 Internal Server Error

    500内部服务器错误

  • 504 Gateway Timeout. Server didn’t receive timely response

    504网关超时。 服务器未及时收到回复

Less known 4xx suggests that you are passing wrong parameter. Can also pass information that is wrong. E.g.

鲜为人知的4xx表示您传递了错误的参数。 也可以传递错误的信息。 例如

DELETE /cars/MH09234

DELETE /cars/MH09234

returns 4xx or message Expecting int car id /car/id got string car/MH09234

返回4xx或消息, Expecting int car id /car/id got string car/MH09234

翻译自: https://www.freecodecamp.org/news/rest-apis/

rest api

相关文章:

0414复利计算6.0--结对

结对同伴:姓名:柯晓君学号:201406114210博客园地址:http://www.cnblogs.com/950525kxj/一、项目简介 开发工具:eclipse 开发语言:java 主要功能:复利单利的计算、贷款的计算以及投资运算三大功能…

把简单做到极致

我真的还没有认真想过我已经是一名即将毕业的大三学生了。关于自己的过去,关于自己的未来。 有时候也有想过好好反思一下自己的过去,却发现自己的过去总是被太多的无奈与遗憾填满。有时候想畅想一下自己的未来,却发现未来总是充满了未知与迷茫…

作为程序员,要取得非凡成就需要记住的15件事。

作为程序员,要取得非凡成就需要记住的15件事。1、走一条不一样的路在有利于自己的市场中竞争,如果你满足于“泯然众人矣”,那恐怕就得跟那些低工资国家的程序员们同场竞技了。2、了解自己的公司以我在医院、咨询公司、物流企业以及大技术公司…

craigslist_Craigslist,Wikipedia和丰富经济

craigslistYou’ve heard it before. Maybe you’ve even said it. “There’s no such thing as a free lunch.”你以前听过 也许你甚至已经说过了。 “没有免费的午餐之类的东西。” “You can’t get something for nothing.”“你不能一无所获。” “Somebody has to pay…

EXCEL基础篇(二)

本章主要内容 一、单元格操作 二、插入批注 三、自动求和 四、填充序列 五、查找、替换 六、对齐方式 七、定位 八、插入形状及设置形状 九、页面设置 一单元格操作 1、插入 a、插入单元格 一个单元格选中状态---右击插入(单元左右移)--即可 b、插入单…

lua5.2调用c函数成功的例子

1. main.c-----------------//动态库#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef _cplusplusextern "C"{#endif#include <lua.h>#include <lauxlib.h>#include <lualib.h>static void checktoptype(lua_St…

【转】Android Activity原理以及其子类描述,androidactivity

Android Activity原理以及其子类描述&#xff0c;androidactivity 简介 Activity是Android应用程序组件&#xff0c;实现一个用户交互窗口&#xff0c;我们可以实现布局填充屏幕&#xff0c;也可以实现悬浮窗口。一个app由很多个Actvitiy组合而成&#xff0c;它们之间用intent-…

python 文件追加写入_Python写入文件–解释了打开,读取,追加和其他文件处理功能

python 文件追加写入欢迎 (Welcome) Hi! If you want to learn how to work with files in Python, then this article is for you. Working with files is an important skill that every Python developer should learn, so lets get started.嗨&#xff01; 如果您想学习如何…

带有中文的字符串各个字符的获取c++程序

简单易懂&#xff0c;上代码&#xff1a; #include <iostream> #include <cstring> #include <string> #include <cstdlib> #include <vector> using namespace std;class CStr{char *c;typedef struct {int start;bool isChinese;} counter;int…

C#时间格式化(Datetime)用法详解

Datetime.ToString(String, IFormatProvider) 参数format格式详细用法&#xff1a; 格式字符关联属性/说明dShortDatePatternDLongDatePatternf完整日期和时间&#xff08;长日期和短时间&#xff09;FFullDateTimePattern&#xff08;长日期和长时间&#xff09;g常规&#xf…

python添加数组元素_Python列表附录–如何向数组添加元素,并附带示例说明

python添加数组元素欢迎 (Welcome) Hi! If you want to learn how to use the append() method, then this article is for you. This is a powerful list method that you will definitely use in your Python projects.嗨&#xff01; 如果您想学习如何使用append()方法&…

学习进度条--第七周

第七周 所花时间&#xff08;包括上课时间&#xff09; 10小时&#xff08;包括上课2小时&#xff09; 代码量&#xff08;行&#xff09; 152 博客量&#xff08;篇&#xff09; 2篇&#xff08;包括团队博客&#xff09; 了解到的知识点 对组内开发的软件进行讨论&am…

Mybatis获取插入记录的自增长ID

转自&#xff1a;http://blog.csdn.net/tolcf/article/details/39035259 1.在Mybatis Mapper文件中添加属性“useGeneratedKeys”和“keyProperty”&#xff0c;其中keyProperty是Java对象的属性名&#xff0c;而不是表格的字段名。 <insert id"insert" parameter…

android中一种不支持的lua操作

今天写了一段lua代码&#xff0c;在win32中正常运行&#xff0c;在android中运行无效。 大概是这样的&#xff1a; ------file1.lua----- local t {} t.str "this is file1.t" return t ---------------------- -----file2.lua------ local t require &quo…

23岁一无所有怎么办_我搬到国外去创业,然后一无所有。

23岁一无所有怎么办以我的名字还不到一美元&#xff0c;它仍然感觉不像是最低点。 (With not even a dollar to my name, it still didn’t feel like rock bottom.) When you tell someone you’re working for a startup, they’ll either think you’re gonna be really ric…

正则表达式的基本入门

一、正则表达式基本语法 1. 两个特殊的符号‘^’和‘$’。他们的作用分别指出一个字符串的开始和结束。 2. 其他还有‘*’&#xff0c;‘’&#xff0c;‘&#xff1f;’这三个符号&#xff0c;表示一个或一序列字符重复出现的次数 "ab{2}" ---表示一个字符串有一个…

多继承中虚基类构造函数的一种调用规则

规则&#xff1a;如果父类中有虚基类(A)&#xff0c;且有一个直接基类(B)是虚基类的子类&#xff0c;那么子类(C或D)若不显式调用虚基类的有参数构造函数&#xff0c;它的直接基类(B)即使在构造列表中调用了非默认构造函数&#xff0c;那么也会直接调用虚基类的默认构造函数。 …

Android 常见异常及解决办法

前言 本文主要记录 Android 的常见异常及解决办法&#xff0c;以备以后遇到相同问题时可以快速解决。 1. java.lang.NullPointerException: Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference 1) …

aws s3 静态网站_如何将静态网站或JAMstack应用托管并部署到AWS S3和CloudFront

aws s3 静态网站S3 and CloudFront are AWS cloud services that make serving static assets powerful and cheap. How can we host a simple static website or JAMstack app on it?S3和CloudFront是AWS云服务&#xff0c;使服务静态资产功能强大且价格便宜。 我们如何在其上…

图像预处理第7步:标准归一化

图像预处理第7步&#xff1a;标准归一化将分割出来的各个不同宽、高的数字字符宽、高统一 //图像预处理第7步&#xff1a;标准归一化 //将分割出来的各个不同宽、高的数字字符宽、高统一 void CChildView::OnImgprcStandarize() {StdDIBbyRect(m_hDIB,w_sample,h_sample);//在…

8. 进制转化的函数

一&#xff0c;表示进制的单词 bin&#xff1a;二进制 oct&#xff1a;八进制 dec&#xff1a;十进制 hex&#xff1a;十六进制二&#xff0c;四种进制的数据表示方式 $bin0b1010; //二进制数字写法&#xff08;暂时不学 &#xff09; …

二叉树广度优先遍历

#include <iostream> using namespace std;struct Node{//二叉树节点int value;Node *left;Node *right; };struct queue{//辅助队列int head;int tail;int len;//队列长度&#xff0c;遍历时用Node ** list;//队列内容void push(Node *n){list[tail] n;len;}Node * pop…

phaser.min.js_如何使用Phaser 3,Express和Socket.IO构建多人纸牌游戏

phaser.min.jsIm a tabletop game developer, and am continually looking for ways to digitize game experiences. In this tutorial, were going to build a multiplayer card game using Phaser 3, Express, and Socket.IO.我是桌面游戏开发人员&#xff0c;并且一直在寻找…

VirtualBox - RTR3InitEx failed with rc=-1912 (rc=-1912)

有一天重启电脑后虚拟机virtual box突然打不开了&#xff0c;提示类似 https://askubuntu.com/questions/900794/virtualbox-rtr3initex-failed-with-rc-1912-rc-1912 参考帖子中查看了一下包的情况dpkg --list virtualbox-* | grep ii 结果&#xff1a;ii virtualbox-dkms …

边工作边刷题:70天一遍leetcode: day 27

Permutation Sequence 原理&#xff1a;一个permutation是n位&#xff0c;在第i位的值取决于有多少个i-1位的组合。这i-1位的组合是在高位pick完之后剩下的数中 细节&#xff1a; 不同于decimal&#xff0c;位数是固定的&#xff0c;所以不能用k>0作为循环条件&#xff08;这…

基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)

#include <iostream> using namespace std; //约定&#xff1a; //1. 图是由很多节点(VERTEX)构成的, 因此图结构是由一个VERTEX的链表构成的, 每个VERTEX则需要有一个id,也就是start, 取start是为了跟LINE更直观地结合。 //2. 每个节点关联着很多(LINE)构成,因此每个VER…

gatsby_如何使用Gatsby和Leaflet创建夏季公路旅行地图绘制应用程序

gatsbyGet ready for the summer by building your own road trip mapping app with this step-by-step guide!通过此逐步指南&#xff0c;构建自己的公路旅行地图应用&#xff0c;为夏天做好准备&#xff01; What are we going to build? 我们要建造什么&#xff1f; What …

NEFU 1146 又见A+B

又见ab Problem:1146 Time Limit:1000ms Memory Limit:65535K Description 给定两个非负整数A,B,求他们的和。 Input 多组输入&#xff0c;每组输入两个非负整数A和B&#xff08;0<A,B<10^3000&#xff09;&#xff0c;可能会有前缀0&#xff0c;但保证总长度不超过3000…

图的最短路径dijkstra算法

想法是这样的&#xff1a; 1. 最开始要建立4个list&#xff0c;分别存储 a. 所有的Vertex: allVertex[] b. 一个空的Vertex list: emptyVertex[] c. 一个前缀表 previous list(用来回溯路径用): previous[] d. 一个表示最短距离的表(就是表示某个点与0点的最短距离)&#xff1…

JDBC数据源连接池(1)---DBCP

何为数据源呢&#xff1f;也就是数据的来源。我在前面的一篇文章《JDBC原生数据库连接》中&#xff0c;采用了mysql数据库&#xff0c;数据来源于mysql&#xff0c;那么mysql就是一种数据源。在实际工作中&#xff0c;除了mysql&#xff0c;往往还会有Oracle&#xff0c;sql se…