1
這是我第一次使用restbed或任何與HTTP相關的東西(編程方式)。 因此,我將restbed添加到了我的項目中,並嘗試運行restbed的github的主頁中顯示的簡單示例。restbed例子
#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);
我已經檢查過啓動函數,使用某種singnal處理程序,但我不知道測試應該如何工作。
有人能幫助我瞭解如果我做錯了什麼,測試是按預期工作還是其他什麼?
謝謝。
編輯:該解釋幫助了很多。所以我嘗試了你發佈的代碼並得到了這個:
$> 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
我猜測「未實現」的消息不是一件好事。你能幫我解決這個問題嗎?
您的更新:你已經註冊了一個POST處理程序,您/資源,但你要發送GET。嘗試做一個POST代替。使用CURL你可以用'--data「做一些事情''並且移除顯式的'-X GET'。 – Rup
非常感謝!這就是訣竅。現在我將嘗試爲json消息設置一個客戶端/服務器,可能我會回來哭泣。 – user1434770