最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【整理】Flask 设计restful的api的返回内容和错误的数据结构

Flask crifan 2468浏览 0评论

rest api messge code

rest api how design messge code

Common REST API Error Codes

REST API HTTP Status Codes and Error Messages Reference

REST API: Messages – Twilio

REST API JSON error code and error message – Stack Overflow

Best Practices for Designing a Pragmatic RESTful API | Vinay Sahni

HTTP status codes

HTTP defines a bunch of meaningful status codes that can be returned from your API. These can be leveraged to help the API consumers route their responses accordingly. I’ve curated a short list of the ones that you definitely should be using:

  • 200 OK – Response to a successful GET, PUT, PATCH or DELETE. Can also be used for a POST that doesn’t result in a creation.

  • 201 Created – Response to a POST that results in a creation. Should be combined with a Location header pointing to the location of the new resource

  • 204 No Content – Response to a successful request that won’t be returning a body (like a DELETE request)

  • 304 Not Modified – Used when HTTP caching headers are in play

  • 400 Bad Request – The request is malformed, such as if the body does not parse

  • 401 Unauthorized – When no or invalid authentication details are provided. Also useful to trigger an auth popup if the API is used from a browser

  • 403 Forbidden – When authentication succeeded but authenticated user doesn’t have access to the resource

  • 404 Not Found – When a non-existent resource is requested

  • 405 Method Not Allowed – When an HTTP method is being requested that isn’t allowed for the authenticated user

  • 410 Gone – Indicates that the resource at this end point is no longer available. Useful as a blanket response for old API versions

  • 415 Unsupported Media Type – If incorrect content type was provided as part of the request

  • 422 Unprocessable Entity – Used for validation errors

  • 429 Too Many Requests – When a request is rejected due to rate limiting

RESTful API Design: what about errors? | Apigee

api-guidelines/Guidelines.md at master · Microsoft/api-guidelines · GitHub

web services – REST API error return good practices – Stack Overflow

10 Best Practices for Better RESTful API | Thinking Mobile

Spring MVC REST Exception Handling Best Practices (part 1)

【总结】

暂时还是用自己的:

{
  “code”: 200,
  “data”: “558788”,
  “message”: “OK”
}
{
  “code”: 10303,
  “message”: “sms code is wrong”
}

吧。

以后也可以借鉴这种:

{
    “status”: 404,
    “code”: 40483,
    “message”: “Oops! It looks like that file does not exist.”
}

转载请注明:在路上 » 【整理】Flask 设计restful的api的返回内容和错误的数据结构

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.201 seconds, using 22.19MB memory