user3049739
user3049739

Reputation: 31

Read http request-headers (Qt/c++)

I'm trying to read http request-headers that I can log into the log file (using Qt/c++). I'm able to read the response headers using following simple code:

QList<QByteArray> headerList = pReply->rawHeaderList();

foreach(QByteArray head, headerList)
{
    qDebug() << head << ":" << pReply->rawHeader(head);
}

pReply->close();

But so far I had no luck with request headers. While looking for the solution I came across this post: Read complete HTTP request-header; But I didn't really understand how to achieve similar functionality with Qt.

I'm bit lost. How should I go about this?

Upvotes: 2

Views: 2600

Answers (2)

mostafa88
mostafa88

Reputation: 542

There is no direct method to get headers of request, but you can get header list and iterate over them and save in a QVariantMap. here is a sample code.

auto reqHeaderName = reply->request().rawHeaderList();
QVariantMap reqHeaders;
for (QString header : reqHeaderName)
{
    reqHeaders.insert(header, reply->request().rawHeader(header.toUtf8()));
}

Upvotes: 0

gmanolache
gmanolache

Reputation: 497

The rawHeader is actually a QPair of QByteArray. See: RawHeader. You either do a for each with the RawHeader instead of QByteArray or just iterate through the list:

    QList<QByteArray> headerList = pReply->rawHeaderList();

    for (int i = 0; i < rawHeaderList.count(); ++i) {
        qDebug() << head << ":" << pReply->rawHeader(i);
    }

    pReply->close();

Upvotes: 2

Related Questions