QNetworkReply::NetworkError(ProtocolInvalidOperationError) what is it and how to fix it?

Viewed 1385

I'm attempting to make a POST request to a web page but I got a error in code below:

void WebViewModel::sendPOST(QString url)
{
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/x-www-form-urlencoded"));
    QByteArray postData;
    postData.append("");
    manager_->post(request, postData);

    connect(manager_, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinishedSlot(QNetworkReply *)));
}

void WebViewModel::replyFinishedSlot(QNetworkReply *reply)
{
    QUrl webReportsUrl("http://...");
    if(reply->error())  <-- **Here I got NetworkReply::NetworkError(ProtocolInvalidOperationError)**
    {
        qDebug() << "Error: ";
        qDebug() << reply->errorString();
        qDebug() << QNetworkReply::NetworkError(reply->error());
        qDebug() << reply->error();
    }
    else
    {
        qDebug() << "no error";
    }
    reply->deleteLater();
}

This is 302 error with a following description from QT documentation:

the requested operation is invalid for this protocol

How to fix?

1 Answers

This happens when server responds with 400 status code (Bad Request) or with 418 status code (I'm a teapot).

Either server is not configured properly, or it does not expect post requests to that endpoint, or it doesn't like requests with empty body.

You can use curl to validate that server responds with no error to post requests.

Related