Comments (2)
为什么? 我也不知道,但是 我们选择了 自定义.
另外, 宜家的api也是永远200, 在meta中自定义一套异常信息.
from leyafo.github.io.
为什么? 我也不知道,但是 我们选择了 自定义.
另外, 宜家的api也是永远200, 在meta中自定义一套异常信息.
- 你内部的具体业务逻辑没必要让外面知道。
- 用户判断 HTTP status code 比解析body更加简单,另外就是你永远返回200是不是要自己去定义一套返回码?
- HTTP 天然适合 CRUD 类的业务,GET PUT DELETE POST,这些已经在 HTTP 协议里面抽象好了,你代码按照这个逻辑去抽象,用户可以无脑使用。
- 你的业务按照 GET PUT DELETE POST, 对路由设计,log 统计,业务抽象更加容易。我这篇文章里 CreateUser 这个例子,就需要在 HTTP SERVER 入口的地方解析一遍body,然后再把数据丢给对应的业务逻辑接口去处理。而且还不能让每个接口去单独序列化它自己需要的数据结构。
对于第四个问题我文章里可能没说太清楚,比如你有一个 create_user 的接口
规范的做法是这个接口应用使用
post /user
{
"name": "foo",
}
HTTP 实现的接口可以是:
create_user(raw_data []byte)
return 201 //数据创建成功
丑陋的做法就是:
json_parse(body) //然后这里面你该返回什么数据结构?
假设你设计了个万能的数据结构。然后,你的API服务里有一个类似这样的switch接口
switch(action){
case "create_user":
create_user(??) //这里你传啥好?,是解析好的json,还是 http 的 raw body?
case "create_foo":
create_user(??) //这里你又该传啥?,如果是解析好的 json,怎么和 create_user 需要的数据结构兼容?
}
return 200 //是你收到了数据,还是数据创建成功了?还是数据创建好了,我需要给你一个ticket?
from leyafo.github.io.
Related Issues (12)
- The problems of Golang init function HOT 1
- GUI 跨平台解决方案-2017 HOT 3
- go 语言操作数据库 CRUD HOT 1
- 我对房地产的思考 HOT 2
- SQLite 背后的故事 HOT 2
- Open source is a business model, not just free beer or software.
- Understanding the dependence in Makefile HOT 1
- Why I learn English so hard HOT 8
- 我的英语学习经验 - 完整版 HOT 11
- Pen and paper are the best writing tools HOT 1
- 我需要什么样的笔记软件? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leyafo.github.io.