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. QHttpMultipart doesn't send the file part

QHttpMultipart doesn't send the file part

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 792 Views 2 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.
  • A Offline
    A Offline
    Antonio Ortiz
    wrote on last edited by
    #1

    Hi, I'm trying to upload an image file to a django restful api, the thing is that django replies with this:

     {
        "image": [
            "The submitted data was not a file. Check the encoding type on the form."
        ]
    }
    

    I follow the Qt documentation to create my multipart request, this is the code:

    void Client::initForFile(QString url)
    {
        responseStatus = false;
    
        request.setUrl(QUrl(url));
        request.setHeader(request.ContentTypeHeader, "multipart/form-data");
    }
    
    bool Client::post(QString url)
    {
        url.replace("file://", "");
        file = new QFile(url);
        QFileInfo fileInfo(url);
    
        if(file->open(QIODevice::ReadOnly))
        {
            multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
            multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
            QHttpPart filePart;
            filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                               QVariant("form-data; name=\"image\""));
            filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
            filePart.setHeader(QNetworkRequest::ContentTypeHeader,
                               QVariant("image/"+fileInfo.suffix().toLatin1()));
    
            filePart.setBodyDevice(file);
            file->setParent(multiPart);
            multiPart->append(filePart);
    
            rawResponse = manager->post(request,multiPart);
            connect(rawResponse, &QNetworkReply::finished,
                    this, &Client::responseRecieved);
    
            multiPart->setParent(rawResponse);
    
            return true;
        }
        qDebug()<<file->errorString();
    
        return false;
    }
    

    is there something I'm missing?. I tried to upload the same image using Postman an it works.

    raven-worxR 2 Replies Last reply
    0
    • A Antonio Ortiz

      Hi, I'm trying to upload an image file to a django restful api, the thing is that django replies with this:

       {
          "image": [
              "The submitted data was not a file. Check the encoding type on the form."
          ]
      }
      

      I follow the Qt documentation to create my multipart request, this is the code:

      void Client::initForFile(QString url)
      {
          responseStatus = false;
      
          request.setUrl(QUrl(url));
          request.setHeader(request.ContentTypeHeader, "multipart/form-data");
      }
      
      bool Client::post(QString url)
      {
          url.replace("file://", "");
          file = new QFile(url);
          QFileInfo fileInfo(url);
      
          if(file->open(QIODevice::ReadOnly))
          {
              multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
              multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
              QHttpPart filePart;
              filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                                 QVariant("form-data; name=\"image\""));
              filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
              filePart.setHeader(QNetworkRequest::ContentTypeHeader,
                                 QVariant("image/"+fileInfo.suffix().toLatin1()));
      
              filePart.setBodyDevice(file);
              file->setParent(multiPart);
              multiPart->append(filePart);
      
              rawResponse = manager->post(request,multiPart);
              connect(rawResponse, &QNetworkReply::finished,
                      this, &Client::responseRecieved);
      
              multiPart->setParent(rawResponse);
      
              return true;
          }
          qDebug()<<file->errorString();
      
          return false;
      }
      

      is there something I'm missing?. I tried to upload the same image using Postman an it works.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @Antonio-Ortiz
      i suggest you compare the (raw) working request with the non-working request from Qt using Fiddler or Wireshark for example.
      This should lead quickly to the cause

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Antonio Ortiz
        wrote on last edited by
        #3

        @raven-worx said in QHttpMultipart doesn't send the file part:

        request

        Thanks for your quick reply, I'll try it

        1 Reply Last reply
        0
        • A Antonio Ortiz

          Hi, I'm trying to upload an image file to a django restful api, the thing is that django replies with this:

           {
              "image": [
                  "The submitted data was not a file. Check the encoding type on the form."
              ]
          }
          

          I follow the Qt documentation to create my multipart request, this is the code:

          void Client::initForFile(QString url)
          {
              responseStatus = false;
          
              request.setUrl(QUrl(url));
              request.setHeader(request.ContentTypeHeader, "multipart/form-data");
          }
          
          bool Client::post(QString url)
          {
              url.replace("file://", "");
              file = new QFile(url);
              QFileInfo fileInfo(url);
          
              if(file->open(QIODevice::ReadOnly))
              {
                  multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                  multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
                  QHttpPart filePart;
                  filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                                     QVariant("form-data; name=\"image\""));
                  filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
                  filePart.setHeader(QNetworkRequest::ContentTypeHeader,
                                     QVariant("image/"+fileInfo.suffix().toLatin1()));
          
                  filePart.setBodyDevice(file);
                  file->setParent(multiPart);
                  multiPart->append(filePart);
          
                  rawResponse = manager->post(request,multiPart);
                  connect(rawResponse, &QNetworkReply::finished,
                          this, &Client::responseRecieved);
          
                  multiPart->setParent(rawResponse);
          
                  return true;
              }
              qDebug()<<file->errorString();
          
              return false;
          }
          

          is there something I'm missing?. I tried to upload the same image using Postman an it works.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @Antonio-Ortiz said in QHttpMultipart doesn't send the file part:

          filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("form-data"));
          filePart.setHeader(QNetworkRequest::ContentTypeHeader,
          QVariant("image/"+fileInfo.suffix().toLatin1()));

          also you are overwriting the ContentType header here

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Antonio Ortiz
            wrote on last edited by
            #5

            Thanks for your answers, I found the problem, I forgot to set the filename to the file path in the header. I changed it to this:

            filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                                       QVariant("form-data; name=\"image\"; filename=\""+fileInfo.filePath()+"\""));
            

            Thanks for the Wireshark advice @raven-worx, it was really helpful

            1 Reply Last reply
            0

            • Login

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