Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

post with qt



  • Hi
    I am trying to make a post using qt with the following code:

    QNetworkAccessManager manager;

    QUrl url("http://192.168.4.1/post");
    QNetworkRequest request(url);
    
    request.setRawHeader("Content-Type", "text/plain");
    
    QByteArray data("Hi");
    QNetworkReply *reply = manager.post(request, data);
    
    if(reply->error()){
        qDebug() << reply->errorString();
    }
    
    connect(reply, &QIODevice::readyRead, this, [=] {
        qDebug() << "Reply: " << reply->readAll();
    });
    

    The thing is that my server don't reply so i don´t know if I am posting with a incorrect format or if it is another problem.

    When I use postman it works just fine!

    The curl code with postman is this one:
    curl --location --request POST 'http://192.168.4.1/post'
    --header 'Content-Type: text/plain'
    --data-raw 'Hi'



  • Try out:

    QUrl url("http://192.168.4.1/post");
    QNetworkRequest request(url);
    
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    
    QUrlQuery urlq;
    urlq.addQueryItem ("Hi", "");
    
    QNetworkReply *reply = manager.post(request, urlq.toString (QUrl::FullyEncoded).toUtf8 ());
    
    while (!reply->isFinished())
    {
    	qApp->processEvents();
    }
    
    if(reply->error ()) qDebug ()<<reply->error();
    else qDebug ()<<reply->readAll();
    
    reply->deleteLater ();
    
    

Log in to reply