Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending HTTP Header Info with Qt QNetworkAccessManager

Tags:

qt

I have the following code, and I would like to add some HTTP header info along with the call. Anyway that I can do that?

void NeoAPI::call(QString apiCall) {
    if (this->ApiCall.contains(apiCall)) {
        QNetworkAccessManager* manager = new QNetworkAccessManager(0);
        connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netReplyFinished(QNetworkReply*)));
        QUrl url = this->ApiCall[apiCall];
        url.addQueryItem("memberid","76710"); // Set for backdoor debugging
        manager->get(QNetworkRequest(url));
    } else {
        this->requestResultText = QString("Call %1 doesn't exist").arg(apiCall);
    }
}

void NeoAPI::netReplyFinished(QNetworkReply *netReply) {
    if (netReply->error() == QNetworkReply::NoError) {
        this->requestResultText = netReply->readAll();
    } else {
        this->requestResultText = "API Call Failed";
    }
    QMessageBox messageBox;
    messageBox.setText(this->requestResultText);
    messageBox.exec();
    //delete netReply;
}

Also, if I wasn't using these inside a class, what would the this in the connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netReplyFinished(QNetworkReply*))); be?

Thanks!

like image 629
Chiggins Avatar asked Jan 21 '23 19:01

Chiggins


1 Answers

Yes, see the documentation of QNetworkRequest.

You'll want to do something like:

QNetworkRequest request(url);
request.setHeader( QNetworkRequest::ContentTypeHeader, "some/type" );
request.setRawHeader("Last-Modified", "Sun, 06 Nov 1994 08:49:37 GMT");
manager->get( header );

Also, if I wasn't using these inside a class, what would the this in the connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(netReplyFinished(QNetworkReply*))); be?

It wouldn't be anything. To connect a signal to a slot, that slot must be a member function of some object. The Qt primer on signals and slots explains this.

like image 83
Casey Avatar answered Jan 25 '23 14:01

Casey