RESTful APIサーバー
QHttpServer を使用して RESTful API サーバーを作成する方法の例です。
この例では、QHttpServer クラスを使用して、小さなアプリケーションでシンプルな RESTful Web API を作成し、ホストする方法を示します。このサーバーは REST スタイルで呼び出しを受け付け、クライアント側で対応する例のRESTful Color Palette API クライアントと一緒に使用することができます。
RESTの制約に従うアプリケーションは、非公式にRESTfulと表現されることがある。RESTful API Serverは、色(Reqres APIと互換性のある未知のリソース)とユーザーの作成、読み取り、更新、および削除操作を可能にします。RESTful API Serverはログイン/ログアウト機能も提供する。この例はReqres APIをベースにしている。
サーバアプリケーションを実行するには、サーババイナリを実行する:
./colorpaletteserver
または
./colorpaletteserver --port 1234
オプションのport
パラメータを指定することで、サーバを実行するポートを指定することができる。
httpServer.route( QString("%1").arg(apiPath), QHttpServerRequest::Method::Get, [&api](const QHttpServerRequest &request) { return api.getPaginatedList(request); });
上記の例では、GET メソッドにルートが指定されており、ページ分割されたアイテムリストが格納された JSON 配列を返します。これを実現するために、QHttpServer::route() メソッドをQHttpServerRequest::Method::Get 列挙型で使用しています。
httpServer.route(QString("%1/<arg>").arg(apiPath), QHttpServerRequest::Method::Get, [&api](qint64 itemId) { return api.getItem(itemId); });
エンティティのリストから単一のアイテムを取得するには、リクエストクエリでアイテムIDを渡します。
httpServer.route(QString("%1").arg(apiPath), QHttpServerRequest::Method::Post, [&api, &sessionApi](const QHttpServerRequest &request) { if (!sessionApi.authorize(request)) { return QHttpServerResponse( QHttpServerResponder::StatusCode::Unauthorized); } return api.postItem(request); });
この例では、ルートはPOSTメソッドを受け入れ、アイテムリストに新しいエントリを追加し、追加されたエントリを表すJSONオブジェクトを返します。このリクエストは認可されなければなりません。リクエストを認可するには、TOKEN
ヘッダーの値が、api/login
またはapi/register
メソッドから以前に返されたトークンと等しくなければなりません。
QHttpServerResponse postItem(const QHttpServerRequest &request) { const std::optional<QJsonObject> json = byteArrayToJsonObject(request.body()); if (!json) return QHttpServerResponse(QHttpServerResponder::StatusCode::BadRequest); const std::optional<T> item = factory->fromJson(*json); if (!item) return QHttpServerResponse(QHttpServerResponder::StatusCode::BadRequest); if (data.contains(item->id)) return QHttpServerResponse(QHttpServerResponder::StatusCode::AlreadyReported); const auto entry = data.insert(item->id, *item); return QHttpServerResponse(entry->toJson(), QHttpServerResponder::StatusCode::Created); }
POSTメソッドは、JSONオブジェクトとして新しいエントリーを返すだけでなく、異なるHTTPステータスコードも返します。新しいエントリーの場合はCreated
、既存のエントリーの場合はAlreadyReported
。この例では、QHttpServerResponse::QHttpServerResponse のオーバーロードを使用して、JSON オブジェクトとそれに対応する HTTP ステータスコードを送信します。
エントリーを作成するには、リクエストボディは、email
first_name
last_name
avatar
フィールドを持つJSONオブジェクトでなければなりません。たとえば
{ "email": "jane.doe@qt.io", "first_name": "Jane", "last_name": "Doe", "avatar": "/img/faces/1-image.jpg" }
ファイル:
- colorpalette/CMakeLists.txt
- colorpalette/apibehavior.h
- colorpalette/colorpalette.pro
- colorpalette/main.cpp
- colorpalette/types.h
- colorpalette/utils.h
画像:
- colorpalette/assets/img/1-image.jpg
- colorpalette/assets/img/10-image.jpg
- colorpalette/assets/img/11-image.jpg
- colorpalette/assets/img/12-image.jpg
- colorpalette/assets/img/2-image.jpg
- colorpalette/assets/img/3-image.jpg
- colorpalette/assets/img/4-image.jpg
- colorpalette/assets/img/5-image.jpg
- colorpalette/assets/img/6-image.jpg
- colorpalette/assets/img/7-image.jpg
- colorpalette/assets/img/8-image.jpg
- colorpalette/assets/img/9-image.jpg
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.