http request get/post
-
Hi,
I try send post request form qt appliacation . I need send request like below:curl --request POST \ --url http://192.168.1.33:22222/req \ --header 'Content-Type: application/json' \ --header 'deviceType: arf tuf_r1' \ --data '{ "number":"33" }'The post function should look like below :
void NetworkWorker::post(QString location) { QJsonObject obj; obj["number"] = "33"; QNetworkRequest request = QNetworkRequest(QUrl(location)); request.setHeader(QNetworkRequest::ContentTypeHeader, "Content-Type: application/json"); request.setRawHeader("deviceType", "arf tuf_r1"); QNetworkReply *reply = manager.post(request, QJsonDocument(obj).toJson()); connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::_readyRead); }does my function in qt correspond to curl notation? I'm not sure about the header .....
-
@JonB Thanks for response, What do you think when I should delete
replyobject ? It necessery ? If function will be called cyclicly that may be crash my app , without deletereply?connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } ); -
Hi,
I try send post request form qt appliacation . I need send request like below:curl --request POST \ --url http://192.168.1.33:22222/req \ --header 'Content-Type: application/json' \ --header 'deviceType: arf tuf_r1' \ --data '{ "number":"33" }'The post function should look like below :
void NetworkWorker::post(QString location) { QJsonObject obj; obj["number"] = "33"; QNetworkRequest request = QNetworkRequest(QUrl(location)); request.setHeader(QNetworkRequest::ContentTypeHeader, "Content-Type: application/json"); request.setRawHeader("deviceType", "arf tuf_r1"); QNetworkReply *reply = manager.post(request, QJsonDocument(obj).toJson()); connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::_readyRead); }does my function in qt correspond to curl notation? I'm not sure about the header .....
@Damian7546 I am not an expert here, but it looks right to me... :)
-
@JonB Thanks for response, What do you think when I should delete
replyobject ? It necessery ? If function will be called cyclicly that may be crash my app , without deletereply? -
@JonB Thanks for response, What do you think when I should delete
replyobject ? It necessery ? If function will be called cyclicly that may be crash my app , without deletereply?connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } ); -
@JonB said in http request get/post:
connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );
In NetworkWorker constructor it may be ?
I get warrning:
Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda] -
@JonB said in http request get/post:
connect(&manager, &QNetworkAccessManager::finished, [](QNetworkReply* reply)->void { reply->deleteLater(); } );
In NetworkWorker constructor it may be ?
I get warrning:
Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda]@Damian7546 said in http request get/post:
In NetworkWorker constructor it may be ?
No, because
replydoes not exist there.Pass a context object as 3rd connect parameter [clazy-connect-3arg-lambda]Ignore or pass I guess
thisor maybe&manageras context object for slot to shut it up. -
@JonB ok, please tell me if the notation below
connect(&manager, &QNetworkAccessManager::finished, this, [&](){reply->deleteLater();});inside my function where
replyobject is created ,works the same like yours proposition ? -
@JonB ok, please tell me if the notation below
connect(&manager, &QNetworkAccessManager::finished, this, [&](){reply->deleteLater();});inside my function where
replyobject is created ,works the same like yours proposition ?@Damian7546 Yep, looks fine.
-
@Damian7546 Yep, looks fine.
-
@Damian7546
Look in the debugger for the reason for the crash. Make surereplyis not null. Make sure you have not deleted it more than once. You may be able to attach to itsQObject::destroyed()signal to see when that happens. -
@Damian7546
Look in the debugger for the reason for the crash. Make surereplyis not null. Make sure you have not deleted it more than once. You may be able to attach to itsQObject::destroyed()signal to see when that happens.@JonB Your solution works. ...
