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"
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.
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,
_
Re: how to login to the application using JSON data in Qt
Code:
serviceURL.setUrl("https://10.201.58.87:2342/kirk/1.0/session");
QNetworkRequest request(serviceURL);
QNetworkAccessManager manager;
QNetworkReply* reply;
request.setRawHeader("Host","10.201.58.88:2342");
request.setRawHeader("host_key", "824D10304899A");
request.setRawHeader("User-Agent", "Mozilla/Firefox ");
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");
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded;charset=UTF-8");
request.setRawHeader("Content-Length", "46");
request.setRawHeader("Connection", "keep-alive");
QByteArray jsonString
= "{\"uname\":\"" +usr
+"\", \"pwd\":\""+pwd
+"\"}";
connect(&manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
reply = manager.post(request, jsonString);
qDebug() << "post" << reply;
qDebug() << "get" << manager.get(request);
qDebug() << " response" << response;
QUrl Url
("https://10.201.58.87:2342/kirk/1.0/hosts/0" );
reply = manager.get(QNetworkRequest(Url));
reply->ignoreSslErrors();
loop.exec();
qDebug() << "error" <<reply->errorString();
//parse json
qDebug() << "URL Reply:" << strReply;
return 1;
got this :
post QNetworkReplyHttpImpl(0x3f60a40)
get QNetworkReplyHttpImpl(0x3f60b80)
response ""
error "Unknown error"
URL Reply: ""
error "Unknown error"
URL Reply: ""
Re: how to login to the application using JSON data in Qt
Hint: what is wrong here (taken directly from your code)?
Code:
reply = manager.post(request, jsonString);
loop.exec();
Cheers,
_