Home > database >  Qt: How to pass parameters in url to rest api in Qt?
Qt: How to pass parameters in url to rest api in Qt?

Time:07-02

I need to connect to Rest API on the server in Qt and I can get token and authenticate but there is a problem and that is when i'm sending some parameters to Rest API like this :"http//localhost:44444/api/Getmojodi?Code=1234" it returns empty response. What is the solution? parameters are not in URL body just "Username","Password" and "grant_type" are in body.

QNetworkAccessManager *man1=new QNetworkAccessManager(this);
connect(man1,&QNetworkAccessManager::finished,this,&MainWindow::authenReply);
QUrl url("http://localhost:59444/api/Getmojodi?KalaCode=20101010131310");
const QByteArray basic_authorization =token.toUtf8().toBase64();
//request.setRawHeader(QByteArrayLiteral("Authorization"), basic_authorization);
request.setUrl(url);
man1->get(request);

CodePudding user response:

To add query fields to a url, I use QUrlQuery. You can add as many as you like.

QUrl url("http://localhost:59444/api/Getmojodi");
QUrlQuery query;
query.addQueryItem("KalaCode", "20101010131310");
query.addQueryItem("someParam", someVal);
query.addQueryItem("otherParam", "otherVal");
url.setQuery(query);
request.setUrl(url);
man1->get(request);

CodePudding user response:

For this problem I checked API request in postman and there was hidden headers that I didn't know about them and added this headers to my request in qt and it worked very well.code is like this :

QUrl url("http://localhost:59444/api/Getmojodi");

request.setRawHeader(QByteArrayLiteral("Authorization"),token.toUtf8());
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRawHeader(QByteArrayLiteral("User-Agent"),"ApiTest2/1.0");
request.setRawHeader(QByteArrayLiteral("Accept"),"*/*");
request.setRawHeader(QByteArrayLiteral("Accept-Encoding"),"gzip, deflate, br");
request.setRawHeader(QByteArrayLiteral("Connection"),"keep-alive");
request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
QUrlQuery uq;
uq.addQueryItem("KalaCode","20101010131310");
url.setQuery(uq);
request.setUrl(url);
manager->get(request);

Than you for your help

  • Related