How to use QHttpServer missing handler?
-
Hi, I cannot figure out, how to use the missing handler in
QHttpServer
. My server://QT = core httpserver #include <QCoreApplication> #include <QDebug> #include <QHttpServer> void missingHandler(const QHttpServerRequest & request, QHttpServerResponder && responder) { qWarning() << "invalid http request" << request.url().path(); responder.writeBody("Oh no! Invalid request!"); responder.write(QHttpServerResponder::StatusCode::NotFound); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QHttpServer server; server.setMissingHandler(missingHandler); server.route("/foo", [] () { return "foo"; }); server.listen(QHostAddress::Any, 1234); qInfo() << "server running"; return a.exec(); }
What it does:
$ curl localhost:1234/foo foo $ curl localhost:1234/bar curl: (1) Received HTTP/0.9 when not allowed
What am I doing wrong? I can't find any example where a missing handler is used.
Thank you for your help. -
Hi, I cannot figure out, how to use the missing handler in
QHttpServer
. My server://QT = core httpserver #include <QCoreApplication> #include <QDebug> #include <QHttpServer> void missingHandler(const QHttpServerRequest & request, QHttpServerResponder && responder) { qWarning() << "invalid http request" << request.url().path(); responder.writeBody("Oh no! Invalid request!"); responder.write(QHttpServerResponder::StatusCode::NotFound); } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QHttpServer server; server.setMissingHandler(missingHandler); server.route("/foo", [] () { return "foo"; }); server.listen(QHostAddress::Any, 1234); qInfo() << "server running"; return a.exec(); }
What it does:
$ curl localhost:1234/foo foo $ curl localhost:1234/bar curl: (1) Received HTTP/0.9 when not allowed
What am I doing wrong? I can't find any example where a missing handler is used.
Thank you for your help.@Tarae
It would help if you said whether yourmissingHandler()
gets called or not!While you await a better C++ expert than I: where you call
server.setMissingHandler(missingHandler);
do you not need to wrapmissingHandler
in astd::function
? The signature requiresstd::function<void(const QHttpServerRequest &request, QHttpServerResponder &&responder)>.
I'm not sure that a plain function name is the same thing as that. Then again, you didn't geta compile error, so who knows.....
Does something like:
server.setMissingHandler([]() { qDebug() << "Help me"; })
get through?
-
@Tarae
It would help if you said whether yourmissingHandler()
gets called or not!While you await a better C++ expert than I: where you call
server.setMissingHandler(missingHandler);
do you not need to wrapmissingHandler
in astd::function
? The signature requiresstd::function<void(const QHttpServerRequest &request, QHttpServerResponder &&responder)>.
I'm not sure that a plain function name is the same thing as that. Then again, you didn't geta compile error, so who knows.....
Does something like:
server.setMissingHandler([]() { qDebug() << "Help me"; })
get through?
-
@JonB It calls the function, the message 'invalid http request...' is printed out to the console. Just curl doesn't like the response.
@Tarae
Oh, you mean just you want to know what to put in the body ofmissingHandler()
to not cause that. I did not understand that was your issue from the question.Since Content-type will be "application/x-empty" I don't suppose just removing
responder.writeBody()
makes it any better?I take it you know what curl will show.
Anyway I leave it to you now, sorry.