9

后端 response code 该怎样返回?

 2 years ago
source link: https://www.v2ex.com/t/808610
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

后端 response code 该怎样返回?

  hlayk · 43 分钟前 · 224 次点击

前提:前后端定义了通用的数据结构包含 data,mseeage,code,success 这几个基本字段用来去接收后端接口的数据。

场景:某一接口客户端发出的请求出现参数或后端校验等业务错误

问题:后端 http 层的 response code 应该返回什么? 200 还是非 200 ?(呆过的不同公司有着不一样的习惯)

问题来源:主要是用的网络请求框架( retrofit ),对于小于 200 或者大于等于 300 的 response code 直接抛了异常,但我并不想对这异常再进行解析 HttpException 中的 response body 解析出上述的数据结构,所以我拦截了 response 改了 response code 为 200 。这样的话,我对于 response code 无论是什么,我都可以同样的对象解析。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK