RESTful API Design: 13 Best Practices to Make Your Users Happy
RESTful API Design: 13 Best Practices to Make Your Users Happy
First step to the RESTful way: make sure errors don't come back as 200 OK.
Web services have been around for as long as the HTTP protocol has existed. But especially since the advent of cloud computing, they have become a very common way of letting clients interact with our data.
I haven't been lucky enough to be a developer when SOAP APIs were still around. So I've mostly known REST, a resource-based architectural style for building APIs.
For a year or two already, I have been working on projects involving either building or using APIs.
Most of the APIs I've seen claimed to be "RESTful" — i.e. compliant with the principles and constraints of REST architecture.
Yet, some of them have been giving REST a very, very bad name.
Bad usage of status codes, plain text responses, inconsistent schemas… I've seen it all (or at least, a good bunch). So I've decided to write up what I think are some good practices when designing REST APIs.
#Disclaimer
I have no authority to say that the following practices comply 100% with the holy REST principles (if there is such a thing!). I gathered those from experience building and working with various APIs.
Likewise, I do not pretend to have mastered REST API design! It is a craft — the more you practice, the better you get.
I will expose some code snippets as "examples of bad design". If they look like something you'd write, it's okay! The only thing that matters is that we learn together. I hope this modest listicle will help us do just that.
Onwards: here are tips, advice and recommendations to design REST APIs that make your users happy.
#1. Learn the basics of HTTP applied to REST
If you're to build a well-designed REST API, you'd better know the basics of the HTTP protocol. I truly believe this will help you make better design decisions.
I find the Overview of HTTP on the MDN web docs to be a very good read for this. However, as far as REST API design is concerned, here's a TL;DR of HTTP applied to REST:
- HTTP has verbs (or methods): GET, POST, PUT, PATCH and DELETE are the most common.
- REST is resource-oriented and a resource is represented by an URI:
/newspapers/
. - An endpoint is the combination of a verb and an URI, e.g.
GET: /articles/
. - An endpoint can be interpreted as an action on a resource. For example,
POST: /articles/
may mean "Create a new article". - At a high-level, verbs map to CRUD operations:
GET
meansRead
,POST
meansCreate
,PUT
andPATCH
meanUpdate
, andDELETE
means... well,Delete
. - A response's status is specified by its status code:
1xx
for information,2xx
for success,3xx
for redirection,4xx
for client errors and5xx
for server errors.
Of course you can use anything the HTTP protocol offers for REST API design, but these are basic things I believe you need to keep in mind.
#2. Don't return plain text
Although it is not imposed by the REST architectural style, most REST APIs use JSON as a data format.
However, it is not enough to return a body containing a JSON-formatted string. You need to specify the Content-Type
header too! It must be set to the value application/json
.
This is especially important for programmatic clients (for example, someone or a service interacting with your API via the requests
library in Python) — some of them rely on this header to correctly decode the response.
Pro tip: you can verify a reponse's Content-Type
very easily with Firefox. It has built-in pretty-display for responses with Content-Type: application/json
.
RESTful API Design: 13 Best Practices to Make Your Users Happy的更多相关文章
- Principles of good RESTful API Design 好的 RESTful API 设计
UPDATE: This post has been expanded upon and converted into an eBook. Good API design is hard! An AP ...
- RESTful API Design With NodeJS & Restify
http://code.tutsplus.com/tutorials/restful-api-design-with-nodejs-restify--cms-22637 The RESTful API ...
- RESTful API Design
https://restful-api-design.readthedocs.org/en/latest/scope.html http://blog.mashape.com/30-ways-to-m ...
- RESTful API 最佳实践(转)
原文:http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html 阮一峰老师的文章,他的文章把难懂的东西讲的易懂 RE ...
- 【转】最佳Restful API 实践
原文转自:https://bourgeois.me/rest/ REST APIs are a very common topic nowaday; they are part of almost e ...
- RESTful API的设计原则
好RESTful API的设计原则 说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间, ...
- RESTful API 设计指南【转】
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
- 好RESTful API的设计原则
说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...
- Django RESTful API 设计指南
网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...
随机推荐
- LOJ2461 完美的队列 分块
传送门 如果对于每一个操作\(i\)找到这个操作中所有的数都被pop掉的时间\(ed_i\),那么剩下就直接差分覆盖一下就可以了. 那么考虑如何求出\(ed_i\).发现似乎并没有什么数据结构能够维护 ...
- 警告:MySQL-server-5.6.26-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
警告:MySQL-server--.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 错误:依赖检测失败: /usr/bin/ ...
- IDEA远程调试Ambari Server
1.配置端口 Ambari Server默认配置了服务端的debug参数,端口为5005.如果要修改端口,可以在/usr/sbin/ambari_server_main.py文件中对应地方修改,直接改 ...
- 华为、华三 交换机 开启SNMP 简单网络管理协议
[huawei]snmp-agent //使能SNMP代理 [huawei]snmp-agent sys-info version all //允许所有SNMP 支持 协议 [huawei]snmp- ...
- js获取对象的属性个数
for (var i = 0; i < dt.length; i++) { if (Object.keys(dt[i]).length <= 1) { dt.splice(i, 1); i ...
- Python 将中文、字母转成数字
Outline 把中文汉字或者英文字母或者特殊字符转换成数字. (实质是字符转成对应ASCII码) 转换 将中文汉字转成数字: ord('单个中文汉字') 反转: chr(21704) 将英文字母转成 ...
- Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口
HttpURLConnection是JDK自身提供的网络类,不需要引入额外的jar包.文中所使用到的软件版本:Java 1.8.0_191. 1.服务端 参见Java调用Http接口(1)--编写服务 ...
- C# NPOI 导入与导出Excel文档 兼容xlsx, xls(xf13中已经引用了xlsx的npoi)
这里使用的NPOI版本为: 2.1.3.1 官方下载地址: http://npoi.codeplex.com/releases 版本内包含.Net 2.0 与.Net 4.0 .Net 4.0中包含文 ...
- js-Date对象(九)
一.Date对象的创建1.new Date()[创建当前时间对象]eg: var date = new Date(); console.log(date); //Thu Jul 18 2019 18: ...
- Telnet入侵Windows2000
开启Telnet 打开控制面板,管理工具 计算机管理 连接刚刚探测到的主机 输入探测到的主机IP 如下图所示,连接成功 找到Telnet服务 启动Telnet服务 远程登录 注意 Telnet登录需要 ...