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.
multipart/form-data
is generally used for file uploads, for simple formsapplication/x-www-form-urlencoded
is enough. Maybe api doesn't even support multipart.