How to send an http POST request with a picture
Solved
General and Desktop
-
Hi!
How to send an http POST request with a picture?
I tried this:QHttpMultiPart * data = new QHttpMultiPart(); QHttpPart part; part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\"")); part.setBody("Hello there!"); data->append(part); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("multipart/form-data; name=\"image\"; filename=\"1.jpg\"")); imagePart.setRawHeader("Content-Transfer-Encoding","binary"); QFile *file = new QFile("E:/images/devine_icons_part_2_by_ipapun-d2c0w18/Gaia/PNG/Dark/1.png"); if(file->open(QIODevice::ReadOnly)) qDebug()<<"file opened"; qDebug()<<"file:"<<file->size(); imagePart.setBodyDevice(file); data->append(imagePart); file->close(); myApiQuery.post(request, data);
But I get an error: QIODevice::read (QFile, "E:\images\devine_icons_part_2_by_ipapun-d2c0w18\Gaia\PNG\Dark\1.png"): device not open
-
Hi,
You are closing before the request could even be processed.
Look at the example of the QHttpMultiPart documentation.