我是REST的新手,我已经尝试运行Restbed的基本教程代码,如下所示:
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void get_method_handler( const shared_ptr< Session > session )
{
session->close( OK, "Hello, World!", { { "Content-Length", "13" }, { "Connection", "close" } } );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "GET", get_method_handler );
//auto ssl_settings = make_shared< SSLSettings >( );
//ssl_settings->set_http_disabled( true );
//ssl_settings->set_private_key( Uri( "file:///tmp/server.key" ) );
//ssl_settings->set_certificate( Uri( "file:///tmp/server.crt" ) );
//ssl_settings->set_temporary_diffie_hellman( Uri( "file:///tmp/dh768.pem" ) );
auto settings = make_shared< Settings >( );
//settings->set_ssl_settings( ssl_settings );
Service service;
service.publish( resource );
service.start( settings );
return EXIT_SUCCESS;
}请注意: SSL init已被注释掉。当我只发出一个基本请求(http://localhost/resource)时,一切都是正常的。对于包含一个参数(http://localhost/resource?request=test)的稍微复杂一点的请求,它仍然可以正常工作。当我试图传入JSON对象(http://localhost/resource?request={})时,我得到了一个错误消息:“您的客户端发出了一个格式错误或非法的请求状态行。这就是我们所知道的一切。”我可以通过从Postman而不是浏览器运行这个命令来解决这个问题,但即使我将命令更改为http://localhost/resource?request={"test"=[]},它也会再次以相同的错误结束。
有没有人能解释一下我所关注的问题?我正在运行64位windows 10,在Visual Studio 2017中编译代码。
提前谢谢你
发布于 2019-03-15 09:00:03
当然,这是一个新手的错误:)。邮递员没有为我编码URL,我花了很长时间才意识到这一点。我已经使用https://www.urlencoder.org/对它进行了编码,现在它工作得很好。
Unencoded example: {"values":["1", "2"]}
Encoded example: %7B%22values%22%3A%5B%221%22%2C%20%222%22%5D%7D这篇文章让我大开眼界:https://perishablepress.com/stop-using-unsafe-characters-in-urls/
编辑:我要补充的是,同时我了解到,JSON对象作为请求的主体发送,头部编码为application/JSON要好得多。
据我所知,HTTP请求由URI - header和body组成。URI是请求的地址(http://localhost:5000/test)头部指示类型并可以包含数据,主体包含可以基于头部(二进制、json、文本)着色的数据。
https://stackoverflow.com/questions/55167098
复制相似问题