Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to send an http POST request with a picture
Forum Updated to NodeBB v4.3 + New Features

How to send an http POST request with a picture

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 932 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are closing before the request could even be processed.

      Look at the example of the QHttpMultiPart documentation.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved