Blog

Blog

PHODAL

CoAP 请求、响应与状态码

在写CoAP的物联网系统的时候,发出一个请求

coap get coap://localhost/id/2

结果返回的是

(4.06)  [{"id":2,"value":"is id 2","sensors1":20,"sensors2":21}]

406不就是Not Acceptable么,于是才知道原来CoAP也有状态码

CoAP请求/响应模式

下面是一个GET请求的示例,状态码是2.05

 Client             Server    
  |                  |      
  |   CON [0xbc90]   |      
  | GET /id/1        |      
  |   (Token 0x71)   |      
  +----------------->|      
  |                  |      
  |   ACK [0xbc90]   |      
  |   2.05 Content   |      
  |   (Token 0x71)   |      
  |     "22.5 C"     |      
  |<-----------------+      
  |                  |

相应的对应于4.04也就有了

  Client              Server
     |                  |
     |   CON [0xbc91]   |
     | GET /id/2        |
     |   (Token 0x72)   |
     +----------------->|
     |                  |
     |   ACK [0xbc91]   |
     |  4.04 Not Found  | 
     |   (Token 0x72)   | 
     |   "Not found"    | 
     |<-----------------+ 
     |                  |

于是就去看看HTTP的状态码和CoAP的状态码,发现原来是相似的。

CoAP状态码

一、 Success 2.xx

这一类型的状态码,代表请求已成功被服务器接收、理解、并接受。

  • 2.01 Created
  • 2.02 Deleted
  • 2.03 Valid
  • 2.04 Changed
  • 2.05 Content

二、 Client Error 4.xx

这类的状态码代表了客户端看起来可能发生了错误,妨碍了服务器的处理。

  • 4.00 Bad Request
  • 4.01 Unauthorized
  • 4.02 Bad Option
  • 4.03 Forbidden
  • 4.04 Not Found
  • 4.05 Method Not Allowed
  • 4.06 Not Acceptable
  • 4.12 Precondition Failed
  • 4.13 Request Entity Too Large
  • 4.15 Unsupported Content-Format

三、 Server Error 5.xx

这类状态码代表了服务器在处理请求的过程中有错误或者异常状态发生,也有可能是服务器意识到以当前的软硬件资源无法完成对请求的处理。

  • 5.00 Internal Server Error
  • 5.01 Not Implemented
  • 5.02 Bad Gateway
  • 5.03 Service Unavailable

其他

物联网系统CoAP版进行时

https://github.com/gmszone/iot-coap

关于我

Github: @phodal     微博:@phodal     知乎:@phodal    

微信公众号(Phodal)

围观我的Github Idea墙, 也许,你会遇到心仪的项目

QQ技术交流群: 321689806
comment

Feeds

RSS / Atom

最近文章

关于作者

Phodal Huang

Engineer, Consultant, Writer, Designer

ThoughtWorks 技术专家

工程师 / 咨询师 / 作家 / 设计学徒

开源深度爱好者

出版有《前端架构:从入门到微前端》、《自己动手设计物联网》、《全栈应用开发:精益实践》

联系我: h@phodal.com

微信公众号: 最新技术分享

标签