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

2.5k views Asked by At

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?

2

There are 2 answers

0
mostafa88 On

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()));
}
0
gmanolache On

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();