Consume WS with token authorisation
-
Hello everybody,
In my app, I'm calling a WS to insert data in a remote database.I'm using the following code but I'm encoutring the error "Host requires authentication".
QEventLoop eventLoop; QNetworkAccessManager networkManager; QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); QNetworkRequest networkRequest(QUrl("http://1XX.1XX.1XX.XX:XXXX/api/Data/Post")); QString header = QString("Bearer %1").arg(m_strUserToken); networkRequest.setRawHeader(QByteArray("Authorization"), header.toLocal8Bit()); networkRequest.setRawHeader("accept", "application/json"); networkRequest.setRawHeader("Content-Type", "application/json"); QNetworkReply *networkReply = networkManager.post(networkRequest, postData.toString(QUrl::FullyEncoded).toUtf8()); eventLoop.exec();
Can anyone tell me what I'm wrong. I've really searched and tried many solution but still having the same error.
-
Hello everybody,
In my app, I'm calling a WS to insert data in a remote database.I'm using the following code but I'm encoutring the error "Host requires authentication".
QEventLoop eventLoop; QNetworkAccessManager networkManager; QObject::connect(&networkManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); QNetworkRequest networkRequest(QUrl("http://1XX.1XX.1XX.XX:XXXX/api/Data/Post")); QString header = QString("Bearer %1").arg(m_strUserToken); networkRequest.setRawHeader(QByteArray("Authorization"), header.toLocal8Bit()); networkRequest.setRawHeader("accept", "application/json"); networkRequest.setRawHeader("Content-Type", "application/json"); QNetworkReply *networkReply = networkManager.post(networkRequest, postData.toString(QUrl::FullyEncoded).toUtf8()); eventLoop.exec();
Can anyone tell me what I'm wrong. I've really searched and tried many solution but still having the same error.
@mourad_bilog
I'm afarid I don't know just what you have to do to authenticate, but is it right that your code never uses theheader
you have set up? -
You are right, I've missed this line
networkRequest.setRawHeader(QByteArray("Authorization"), header.toLocal8Bit());