Results 1 to 5 of 5

Thread: how to login to the application using JSON data in Qt

  1. #1
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default how to login to the application using JSON data in Qt

    I am trying to access data from url string https://10.201.58.88:2342/kirk/1.0/h...physicaldrives but it is showing me HOst require Authantication
    so I have to login for my app using JSON Url https://10.201.58.88:2342/kirk/1.0/session
    {
    "user":
    {
    "uname": "root",
    "kirk_access": "ADMIN",
    "hosts":
    [
    {
    "host_key": "824D103048EB0116A",
    "ip": "10.201.58.88",
    "port": "2342",
    "hostname": "localhost.localdomain",
    "is_local_host": "true",
    "auth_type": "host",
    "uname": "root",
    "config_type": "enterprise",
    "sys_access": "ADMIN"

    }
    ]
    },
    how to get Login and access data from https://10.201.58.88:2342/kirk/1.0/h...physicaldrives

    / QString USER_AGENT = "Mozilla/5.0";

    QUrl serviceURL("https://10.201.58.87:2342/kirk/1.0/session");
    QNetworkRequest request(serviceURL);


    request.setRawHeader("Host","10.201.58.88:2342");
    request.setRawHeader("host_key", "824D10304816A");
    request.setRawHeader("User-Agent", "Mozilla/5.0");
    request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    request.setRawHeader("Accept-Language", "en-US,en;q=0.5");
    request.setRawHeader("Accept-Encoding", " gzip, deflate");
    request.setRawHeader("Referer", "https://10.201.58.87:2342/ui/core/index.html");
    request.setRawHeader("Content-Type", "application/json;application/x-www-form-urlencoded");
    request.setRawHeader("Content-Length", "46");
    request.setRawHeader("Connection", "keep-alive");

    QByteArray usr = "root";
    QByteArray pwd = "abc@123";
    QByteArray jsonString = "{\"uname\":\"" +usr+"\", \"pwd\":\""+pwd+"\"}";


    Q QNetworkAccessManager manager;
    QEventLoop loop;
    connect(&manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    reply = manager.post(request, jsonString);


    qDebug() << "post" << reply;
    qDebug() << "get" << manager.get(request);
    QByteArray response = reply->readAll();

    qDebug() << " response" << response;
    loop.exec();
    I have tried this but getting

    Starting ....build-layout-Desktop_Qt_5_5_1_MSVC2013_64bit-Debug\debug\layout.exe...
    post QNetworkReplyHttpImpl(0x2386790)
    get QNetworkReplyHttpImpl(0x365a50)
    response ""
    error "Unknown error"
    Last edited by Radhika; 3rd December 2015 at 10:56.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: how to login to the application using JSON data in Qt

    Depends entirely on the server application. You'll have to find/read/digest the server API documentation to determine how to properly authenticate your requests. Could be simple authentication using HTTP basic authentication (i.e. Authorization HTTP header), or something more complex like OAuth2, etc.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to login to the application using JSON data in Qt

    Your code snippet seems to miss the line where you actually run the nested event loop.

    Cheers,
    _

  4. #4
    Join Date
    Nov 2015
    Location
    India
    Posts
    16
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to login to the application using JSON data in Qt

    Qt Code:
    1.  
    2. QUrl serviceURL;
    3. serviceURL.setUrl("https://10.201.58.87:2342/kirk/1.0/session");
    4.  
    5. QNetworkRequest request(serviceURL);
    6. QNetworkAccessManager manager;
    7. QNetworkReply* reply;
    8.  
    9.  
    10. request.setRawHeader("Host","10.201.58.88:2342");
    11. request.setRawHeader("host_key", "824D10304899A");
    12. request.setRawHeader("User-Agent", "Mozilla/Firefox ");
    13. request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    14. request.setRawHeader("Accept-Language", "en-US,en;q=0.5");
    15. request.setRawHeader("Accept-Encoding", " gzip, deflate");
    16. request.setRawHeader("Referer", "https://10.201.58.87:2342/ui/core/index.html");
    17. request.setRawHeader("Content-Type", "application/json");
    18. request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
    19. request.setRawHeader("Content-Length", "46");
    20. request.setRawHeader("Connection", "keep-alive");
    21.  
    22. QByteArray usr = "root";
    23. QByteArray pwd = "asd@123";
    24. QByteArray jsonString = "{\"uname\":\"" +usr+"\", \"pwd\":\""+pwd+"\"}";
    25.  
    26. connect(&manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    27. reply = manager.post(request, jsonString);
    28.  
    29. qDebug() << "post" << reply;
    30. qDebug() << "get" << manager.get(request);
    31.  
    32. QByteArray response = reply->readAll();
    33. qDebug() << " response" << response;
    34.  
    35. QUrl Url("https://10.201.58.87:2342/kirk/1.0/hosts/0" );
    36. reply = manager.get(QNetworkRequest(Url));
    37. reply->ignoreSslErrors();
    38. loop.exec();
    39.  
    40. qDebug() << "error" <<reply->errorString();
    41. QString strReply = (QString)reply->readAll();
    42.  
    43.  
    44. //parse json
    45. qDebug() << "URL Reply:" << strReply;
    46.  
    47. return 1;
    To copy to clipboard, switch view to plain text mode 

    got this :
    post QNetworkReplyHttpImpl(0x3f60a40)
    get QNetworkReplyHttpImpl(0x3f60b80)
    response ""
    error "Unknown error"
    URL Reply: ""
    error "Unknown error"
    URL Reply: ""

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to login to the application using JSON data in Qt

    Hint: what is wrong here (taken directly from your code)?

    Qt Code:
    1. reply = manager.post(request, jsonString);
    2.  
    3. QByteArray response = reply->readAll();
    4.  
    5. loop.exec();
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. Correct POST html with JSON data
    By Mbded in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2015, 10:19
  2. Using JSON sample application
    By mania in forum Qt Programming
    Replies: 1
    Last Post: 23rd June 2015, 11:12
  3. How to load base64 image data from json in QT
    By phpkode in forum Qt Programming
    Replies: 4
    Last Post: 9th January 2015, 11:10
  4. model/view json data displaying
    By binaural in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2012, 08:13
  5. Qt application settings in JSON
    By asvil in forum Qt-based Software
    Replies: 0
    Last Post: 5th March 2011, 21:35

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.