Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to send an http POST request with a picture



  • 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


  • Lifetime Qt Champion

    Hi,

    You are closing before the request could even be processed.

    Look at the example of the QHttpMultiPart documentation.


Log in to reply