qtqmlqnetworkaccessmanagerqurl

How POST QList of int to my webservice in qt?


I have an QList of int and I want with post request send it to web service

QList<int> list;
QUrlQuery query ;
QNetworkReply* reply= postRequest(query,"www.myapi.com/api/Book/SaveDigits");//a method that will send POST request
connect(reply,SIGNAL(finished()),this,SLOT(getListFinished()));

but I don't know how add this list of interger to QUrlQuery?

I had before post Class Data like below?

QUrlQuery postData;
postData.addQueryItem("UserName",userName());
postData.addQueryItem("Password",password());

I use aps.net webapi2 for my webservice

 [HttpPost]
 public async Task<IHttpActionResult> SaveDigits(UserInfo userInfo)
 {
 }

above code works for post data as `UserInfo' class but when i want to save List it don't work

 [HttpPost]
 public async Task<IHttpActionResult> SaveDigits(List<int> list)
 {
 }

But how can i send QList<int> ?


Solution

  • You can call addQueryItem() with the same key multiple times, the values won't overwrite each others.

    Assuming you want the key to be called "digit", you can use something like this:

    for(int value : list) {
        query.addQueryItem("digit", QString::number(value));
    }
    

    On the server side, most web service frameworks are smart enough to turn the key with multiple values into an array or a list.