Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use QxHttpServer module with QNetworkAccessManager #123

Open
xiaocai123123 opened this issue Feb 27, 2024 · 2 comments
Open

How to use QxHttpServer module with QNetworkAccessManager #123

xiaocai123123 opened this issue Feb 27, 2024 · 2 comments

Comments

@xiaocai123123
Copy link

Hello!
QxHttpServer module is used as http server and QNetworkAccessManager is used as http client. The http client sends a post message ,but the http server cannot receive it.

  1. http server code:
    int main(int argc, char* argv[])
    {
    QCoreApplication app(argc, argv);
    qx::service::QxConnect* serverSettings = qx::service::QxConnect::getSingleton();
    serverSettings->setIp("127.0.0.1");
    serverSettings->setPort(6000);
    serverSettings->setKeepAlive(5000);
    serverSettings->setThreadCount(50);

    qx::QxHttpServer httpServer;
    httpServer.dispatch("POST", "application/json", [](qx::QxHttpRequest& request, qx::QxHttpResponse& response) {
    response.data() = "Hello World !";
    });

    httpServer.dispatch("POST", "", [](qx::QxHttpRequest& request, qx::QxHttpResponse& response) {
    response.data() = "Hello World !";
    });

    httpServer.startServer();
    return app.exec();
    }

  2. http client code:
    QNetworkAccessManager* manager = new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));

QNetworkRequest request;
QNetworkRequest::KnownHeaders header = QNetworkRequest::ContentTypeHeader;
QVariant value = "application/json";
request.setHeader(header, value);
request.setUrl(QUrl("http://127.0.0.1:6000"));
QByteArray data = "Hello world!";
manager->post(request, data);

I can't receive post messages from http clients in lumbda at httpServer.dispatch.I am using Qt's QNetworkAccessManager to post messages in my application. How can I solve this problem? Looking forward to your reply!

@QxOrm
Copy link
Owner

QxOrm commented Feb 27, 2024

Hello,

QxOrm package contains the qxBlogRestApi example project : https://github.com/QxOrm/QxOrm/tree/master/test/qxBlogRestApi
Have you tried it ? Is it working fine ?

This example project uses QxHttpServer module and sends HTTP requests via your default web-browser (Firefox, Chrome or whatever).

And in the documentation here : https://www.qxorm.com/qxorm_en/manual.html#manual_855
We use the tool ab (Apache Benchmark) to simulate a lot of HTTP requests.

So anything which could generate a valid HTTP requests should be able to communicate with the QxHttpServer module.

So I think your issue is to not use the QNetworkAccessManager class correctly.
But this is not a QxOrm issue.
I searched quickly "QNetworkAccessManager http request" on google, maybe this link could help you :
https://stackoverflow.com/questions/60904216/http-post-request-in-qt-using-qnetworkaccessmanager

@xiaocai123123
Copy link
Author

Yes, it's my fault. I should study the demo well. Your example project works well and it is a great example. I think I need to find a way to make it work QNetworkAccessManager with well. Sincere thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants