How to create HTTP request using form-data in QT
-
I am trying to sent an HTTP request to server with key-value pair form-data in it. but I think I missing something while sending request that's why I didn't receive the correct response from server and I am not able to get whats missing in it.
I have an API which generates terrain data and create its file. Here is my curl request
curl --location --request POST 'http://abc.xyz.com:123/generate' \ --header 'Content-Type;' \ --header 'Authorization: Bearer xxxxxxxx-xxxxxxx-xxxxxxx-xxx-xxx-xx-xxxxx' \ --form 'lat="19.3"' \ --form 'long="73.20"' \ --form 'radius="5"'
when I hit the request from postman it gives correct output but when I hit api from code it gives me "\n" as response.
Here is my code for same in QT
void RestApiHelper::generateTerrainData(float lat, float lon, qint32 radius) { QUrl targateUrl = m_apiUrlHelper->getGenerateTerrainURL(); qDebug() << targateUrl; QNetworkRequest request; request.setUrl(targateUrl); QString data = "xxxxx-xxxxxxx-xxxxxxxxx-xxxxxxxx"; QString headerData = m_apiUrlHelper->getApiAuthorizationType() + data; request.setRawHeader( "Authorization", headerData.toLocal8Bit()); qDebug() << "headerData :" << headerData.toLocal8Bit(); QByteArray payload; payload.append("lat", lat); payload.append("long", lon); payload.append("radius", radius); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart textPart; //textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data")); textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\"")); textPart.setBody(payload); multiPart->append(textPart); QNetworkAccessManager *restclient; restclient = new QNetworkAccessManager(); //restclient->post(request,multiPart); QNetworkReply *reply = restclient->post(request, multiPart); multiPart->setParent(reply); disconnect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, nullptr); connect(restclient, SIGNAL(finished(QNetworkReply*)), &m_generateTerrainProcessor, SLOT(handleAPIResponse(QNetworkReply*))); connect(&m_generateTerrainProcessor, SIGNAL(dataReady(QString)), this, SLOT(handleDataReady(QString))); }
Here I sent data in kay/value pair to QByteArray and set this body to multiport. I tried with this way but didnt get expected result. The expected result is link of a generated file.
can anyone please tell me what I am doing wrong or is there any other method to assign kay-value pair form-data to the request in QT.
-
@HemantSuryawanshi said in How to create HTTP request using form-data in QT:
QByteArray payload;
payload.append("lat", lat);
payload.append("long", lon);
payload.append("radius", radius);this does not create a key/value pair!
-
auto json_obj = createJsonObject( key, double_data ); QJsonDocument json_document; json_document.setObject( json_obj ); post( json_document.toJson() );
auto MyClass::createJsonObject( const QString & key, const double value ) ->QJsonObject
{
QJsonObject final_json_obj;
final_json_obj.insert( key, QJsonValue( value ) );
...........
} -
@JoeCFD
how is this related? -
@raven-worx Use json to create key/value pairs and then convert it to QByteArray. That is how I do HTTP request.
-
@JoeCFD
thats how you do http requests on most REST interfaces, yes.
form encoded data is plain key value pairs, not json. -
@raven-worx Sorry. I did not read his code properly.
Does this linlk help?
https://forum.qt.io/topic/56708/solved-qnetworkaccessmanager-adding-a-multipart-form-data-to-a-post-request/6 -
@raven-worx @JoeCFD thanks for your replies. I also tried with json before but get same output.
@JoeCFD I also referred the link you stated before. its for sending file. but I don't want to send a file, I just want to send key/value pair data using form-data.
can you please tell me how can I do this in Qt. -
If your form-data is just plain text you should be able to do that by setting ContentTypeHeader to "application/x-www-form-urlencoded" and then just post a query string, instead of using QHttpMultiPart. Something like
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); restclient->post(request, "lat=19.3&long=73.20&radius=5");
-
@Bonnie it worked. Thanks for your response