Restbed:当我尝试发出包含 JSON 参数的请求时出现问题
Restbed: issue when i try to make a requst containing JSON parameter
我是 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) its still working as intended. When i try to pass in JSON object (http://localhost/resource?request={}) i get an error stating "Your client has issued a malformed or illegal request status line. That’s all we know.". I can get over this issue by running this command from Postman rather than browser but even then as soon as i change the command to http://localhost/resource?request={"test"=[]} 它再次以相同的错误结束。
任何人都可以阐明我在看什么问题吗?
我是运行ning 64位windows10,在Visual Studio2017年编译代码。
提前致谢
当然是新手犯的错误 :)。 Postman 没有为我编码 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 object 最好作为请求的 body 发送,header 编码为 application/JSON.
根据我的理解,HTTP 请求包含 URI - header 和 body。 URI 是请求的地址(http://localhost:5000/test)header 指示类型并且可以包含数据,body 包含可以根据 header(二进制,json, 文字).
我是 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) its still working as intended. When i try to pass in JSON object (http://localhost/resource?request={}) i get an error stating "Your client has issued a malformed or illegal request status line. That’s all we know.". I can get over this issue by running this command from Postman rather than browser but even then as soon as i change the command to http://localhost/resource?request={"test"=[]} 它再次以相同的错误结束。
任何人都可以阐明我在看什么问题吗? 我是运行ning 64位windows10,在Visual Studio2017年编译代码。
提前致谢
当然是新手犯的错误 :)。 Postman 没有为我编码 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 object 最好作为请求的 body 发送,header 编码为 application/JSON.
根据我的理解,HTTP 请求包含 URI - header 和 body。 URI 是请求的地址(http://localhost:5000/test)header 指示类型并且可以包含数据,body 包含可以根据 header(二进制,json, 文字).