【发布时间】:2017-03-02 15:36:09
【问题描述】:
这是我第一次使用 restbed 或任何与 HTTP 相关的东西(编程方面)。 所以我将restbed添加到我的项目中,并尝试运行restbed的github主页中显示的简单示例。
#include <memory>
#include <cstdlib>
#include <restbed>
using namespace std;
using namespace restbed;
void post_method_handler( const shared_ptr< Session > session )
{
const auto request = session->get_request( );
int content_length = request->get_header( "Content-Length", 0 );
session->fetch( content_length, [ ]( const shared_ptr< Session > session, const Bytes & body )
{
fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
session->close( OK, "Hello, World!", { { "Content-Length", "13" } } );
} );
}
int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/resource" );
resource->set_method_handler( "POST", post_method_handler );
auto settings = make_shared< Settings >( );
settings->set_port( 1984 );
settings->set_default_header( "Connection", "close" );
Service service;
service.publish( resource );
service.start( settings );
return EXIT_SUCCESS;
}
它停在一行:service.start(settings);
我检查了启动功能,使用了某种信号处理程序,但我不知道测试应该如何真正工作。
有人可以帮助我了解我是否做错了什么、测试是否按预期工作或其他原因?
谢谢。
已编辑:解释帮助很大。所以我尝试了你发布的代码并得到了这个:
$> curl -w'\n' -v -X GET 'http://localhost:1984/resource'
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 1984 (#0)
> GET /resource HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:1984
> Accept: */*
>
< HTTP/1.1 501 Not Implemented
* no chunk, no close, no size. Assume close to signal end
<
* Closing connection 0
我猜“未实施”消息不是一件好事。你能帮我解决这个问题吗?
【问题讨论】:
-
你的更新:你已经为你的 /resource 注册了一个 POST 处理程序,但是你正在发送 GET。尝试做一个 POST 代替。使用 CURL,您可以使用
--data "something"执行此操作,并删除显式的-X GET。 -
非常感谢!这就成功了。现在我将尝试为 json 消息设置客户端/服务器,可能我会哭泣。