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.

Architect at work. Daniel McCullough, unsplash.com

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 operationsGET means ReadPOST means CreatePUT and PATCH mean Update, and DELETE means... well, Delete.
  • A response's status is specified by its status code1xx for information, 2xx for success, 3xx for redirection, 4xx for client errors and 5xx 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的更多相关文章

  1. 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 ...

  2. RESTful API Design With NodeJS & Restify

    http://code.tutsplus.com/tutorials/restful-api-design-with-nodejs-restify--cms-22637 The RESTful API ...

  3. RESTful API Design

    https://restful-api-design.readthedocs.org/en/latest/scope.html http://blog.mashape.com/30-ways-to-m ...

  4. RESTful API 最佳实践(转)

    原文:http://www.ruanyifeng.com/blog/2018/10/restful-api-best-practices.html 阮一峰老师的文章,他的文章把难懂的东西讲的易懂 RE ...

  5. 【转】最佳Restful API 实践

    原文转自:https://bourgeois.me/rest/ REST APIs are a very common topic nowaday; they are part of almost e ...

  6. RESTful API的设计原则

    好RESTful API的设计原则   说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间, ...

  7. RESTful API 设计指南【转】

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

  8. 好RESTful API的设计原则

    说在前面,这篇文章是无意中发现的,因为感觉写的很好,所以翻译了一下.由于英文水平有限,难免有出错的地方,请看官理解一下.翻译和校正文章花了我大约2周的业余时间,如有人愿意转载请注明出处,谢谢^_^ P ...

  9. Django RESTful API 设计指南

    网络应用程序,分为前端和后端两个部分.当前的发展趋势,就是前端设备层出不穷(手机.平板.桌面电脑.其他专用设备......). 因此,必须有一种统一的机制,方便不同的前端设备与后端进行通信.这导致AP ...

随机推荐

  1. celery 分布式异步任务框架(celery简单使用、celery多任务结构、celery定时任务、celery计划任务、celery在Django项目中使用Python脚本调用Django环境)

    一.celery简介: Celery 是一个强大的 分布式任务队列 的 异步处理框架,它可以让任务的执行完全脱离主程序,甚至可以被分配到其他主机上运行.我们通常使用它来实现异步任务(async tas ...

  2. Python输出菱形

    最近准备熟悉下Python的基础语法,准备练习下输出菱形.刚好作为自己blog的开篇~~ n =10 #控制菱形的大小 for i in range(1, n): for j in range(int ...

  3. SVN客户端教程

    .SVN是一个自由/开源的版本控制系统,一组文件存放在中心版本库,记录每一次文件和目录的修改,Subversion允许把数据恢复到早期版本,或是检查数据修改的历史,Subversion可以通过网络访问 ...

  4. 记录个超级Update语句

    -- UPDATE UPDATE affair_list SET deleteState = WHERE gid IN ( SELECT tt.gid FROM ( SELECT a.gid FROM ...

  5. CSS-锚点笔记

    注意点: position属性 定义建议元素布局所用的定位机制 {position:static/absolute/relative/fixed;} static:默认值,没有定位 absolute: ...

  6. vue 属性props定义方法

    当子组件接收父组件传过来的值的时候,我们一般有两种方式来接收 不过大家好像都用第二种方式,我只有在不确定数据类型的时候才用第一种方式 第一种: export default { // 不检测类型,全盘 ...

  7. tensorflow遇到ImportError: Could not find 'cudart64_100.dll'错误解决

    在安装tensorflow的时候,使用import tensorflow出现了找不到dll文件的错误,参考了很多博客和stackflow的解决方案,发现其中只说了版本号不匹配,但是没有具体说明什么样的 ...

  8. JAVA导出excel 直接弹出下载框

    转自:https://blog.csdn.net/qq_38423105/article/details/80782283 效果展示: 1.首先准备jar包 <dependency>    ...

  9. ugui用户定义操作按键

    界面很简单,只创建了一Image,Image下边有一个Text.基本思路是点击Image,Text清空,进入修改状态,然后用户按下任意键,按下的任意键极为修改后的键 然后下面的脚本是挂在Image下面 ...

  10. [LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)

    描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...