首页 > 解决方案 > 使用微控制器/明文预编码的 POST 字符串制作 HTTP POST (REST API)

问题描述

我正在开发一个微控制器与 Web 服务器接口的项目。它非常接近裸机,因此该项目不允许使用任何库或任何东西。

我已经能够很好地执行 GET 请求,像这样为我的微编码字符串:

"GET /rest/foo/_attribute/bar/ HTTP/1.1\r\n\r\n"

返回的数据看起来像这样:

HTTP/1.1 200 OK                                                               
Date: Thu, 01 Jan 1970 00:01:06 GMT                                           
Connection: Keep-Alive                                                        
Set-Cookie: rtlt4session=1; path=/                                            
Cache-control: no-cache                                                       
Content-Type: application/json                                                
Content-Length: 70
                                                                    
{"bar":"factory":"Attribute","factoryType":"bool","value":true}}`

但是,我在发出 POST 请求时遇到问题。我试过这个:

"POST /rest/foo/_attribute/bar/ HTTP/1.1\r\n\r\n{"bar":"factory":"Attribute","factoryType":"bool","value":false}}\r\n\r\n"

但它不能正常工作,设备通常会等待一段时间然后响应 400 Bad Request 消息,说明缺少 REST 更新内容,如下图所示:

HTTP/1.1 400 Bad Request                                    
Date: Thu, 01 Jan 1970 00:07:23 GMT                         
Connection: Keep-Alive                                      
Set-Cookie: rtlt4session=3; path=/                          
Cache-control: no-cache                                     
Content-Type: text/plain                                    
Content-Length: 84
                                                                                                                        
Missing REST update content on a POST for REST URI '/rest/foo/_attribute/bar'

任何建议或意见,将不胜感激。

标签: resthttppost

解决方案


推荐阅读