Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Consume WS with token authorisation

Consume WS with token authorisation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 322 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mourad_bilog
    wrote on last edited by mourad_bilog
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • M mourad_bilog

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 the header you have set up?

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mourad_bilog
        wrote on last edited by mourad_bilog
        #3

        You are right, I've missed this line

        networkRequest.setRawHeader(QByteArray("Authorization"), header.toLocal8Bit());
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved