私は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);
私はstart関数をチェックしましたが、何らかの種類の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