How to create an HTTP server with QNetworkAccessManager?
-
wrote on 16 Nov 2021, 18:31 last edited by Fahad-Cattchi
I can send a get request on the client side with:
HttpClient::HttpClient() { this->nam = QNetworkAccessManager{}; this->reply = nam.get(QNetworkRequest(QUrl("http://www.example.com"))); connect(reply, &QNetworkReply::finished, this, &HttpClient::replyFinished); } void HttpClient::replyFinished() { doSomethingWith(this->reply); }
But I'm having trouble setting up the server side. I assumed the
QNetworkAccessManager
would have some function for listening for messages on a port like other communication libraries I've used. And I can't figure out how to send a response as a server would, instead of a client. I can only get, put, post, etc... I don't think that I'm supposed to use an http verb in the server response, but I don't know.HttpServer::HttpServer() { this->nam = QNetworkAccessManager{}; connect(&nam, &QNetworkAccessManager::finished, this, &HttpServer::replyFinished); this->nam.listen(80); // Listen for incoming connections. } void HttpServer::replyFinished(QNetworkReply* reply) { doSomethingWith(reply); QNetworkRequest request; request.setRawHeader("content-length", "100"); request.write( ... ); // Put data in the request body somehow. this->nam.respond(url, request); // Respond the way a server should, not a client. }
-
Hi and welcome to devnet,
You have it wrong, QNetworkAccessManager is on the client side of the communication.
Depending on what you want to do on your server, you should consider using something like Cutelyst.
1/2