Nominate our 2022 Qt Champions!

Unable to login using qtnetworkaccessmanager , but able to login using postman tool

  • HI,
    I am using qnetworkaccessmanager to login to a website, i am setting user and password in rowHeader but getting empty response in code, but the same request with user and password set in header works properly using Postman tool, can anyone suggest what could be the issue ?

    QString user = "user";
    	QString pass = "testPassword";
    	QByteArray data1 = user.toLocal8Bit().toBase64();
    	QByteArray data2 = pass.toLocal8Bit().toBase64();
    	QNetworkRequest request = QNetworkRequest(QUrl("http://server_address/b1/login"));
    	request.setRawHeader("user", data1);
    	request.setRawHeader("password", data2);
    	QNetworkReply *reply = manager->get(request);

    and i am fetching reply using below code -

    manager = new QNetworkAccessManager();
    	QObject::connect(manager, &QNetworkAccessManager::finished,
    		this, [=](QNetworkReply *reply) {
    		if (reply->error()) {
    			qDebug() << reply->errorString();
    		QString answer = QString::fromUtf8(reply->readAll());
    		qDebug() << answer;
    		bool isFinished = reply->isFinished();
    		bool isRunning = reply->isRunning();
    		QNetworkReply::NetworkError err = reply->error();
    		QByteArray bts = reply->readAll();
    		QString str(bts);
    		qDebug() << str;

  • Lifetime Qt Champion

    You can try to NOT set
    request.setRawHeader("user", data1);
    request.setRawHeader("password", data2);

    and then use the signal +QAuthenticator

     QObject::connect(&netMan,&QNetworkAccessManager::authenticationRequired,repl,[repl](QNetworkReply *reply, QAuthenticator *authenticator)->void{

    and see if that makes a difference.

  • QNetworkReply *reply = manager->get(QNetworkRequest(tempURL));
    QObject::connect(manager, &QNetworkAccessManager::authenticationRequired, reply, [reply](QNetworkReply *repl, QAuthenticator *authenticator)->void {
    // if (repl != reply)
    // return;

    Tried this, but it is not going inside slot for setting user and password.
    Am I doing something wrong?

  • Lifetime Qt Champion

    It looks correct.
    Maybe it only triggers with HTTPS links.

    Since you use QNetworkAccessManager::finished signal, we can rule out the normal suspects that event loop is not running etc.

    It was worth a shot :) Give it some more hours and maybe someone has an idea of why setRawHeader seems not to work.
    (it should as far as i know)

Log in to reply