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. Trouble posting to a resful API
Forum Update on Monday, May 27th 2025

Trouble posting to a resful API

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmultipartqhttppartqnetworkrequest
4 Posts 2 Posters 759 Views
  • 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.
  • K Offline
    K Offline
    Knox
    wrote on last edited by
    #1

    Following various guides i am having trouble on posting a file ( image) to a resful api that i have created.

        qDebug()<<"Recieved: " + path.replace("file:///","");
    
        QString string = path.replace("file:///","");
    
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
    
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary=" + multiPart->boundary()));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
        QFile *file = new QFile(string);
        if (file->open(QIODevice::ReadOnly))
            qDebug() << "file opened";
        imagePart.setBodyDevice(file);
        file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
    
        multiPart->append(imagePart);
    
        QUrl url("https://mighty-lionfish-83.localtunnel.me/imageUpload");
        QNetworkRequest request(url);
    
        QNetworkAccessManager manager;
        QNetworkReply *reply = manager.post(request, multiPart);
    //    connect(reply,finished() ,this, replyFinished());
        multiPart->setParent(reply); // delete the multiPart with the reply
        // here connect signals etc.
    

    I am sure the server works because Postman gets a successful post. Here is the postman code maybe it may have something useful

    POST /imageUpload HTTP/1.1
    Host: mighty-lionfish-83.localtunnel.me
    cache-control: no-cache
    Postman-Token: 0aaec160-1b35-4cb6-a122-64bdd94726b3
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
    
    Content-Disposition: form-data; name="uploadImage"; filename="C:\Qt_Test\Schedule.JPG
    
    
    ------WebKitFormBoundary7MA4YWxkTrZu0gW--
    

    The problem is it wont throw any error, and the server that i have created does not log any post attempts. i have put various qDebugs and can assures that the function it is in after the last code. If you can help me thank you very much

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      You are not setting the HTTP properties properly. Just copy the following code in your program. It should work. Also if something is problem, it should give network error.

          QFile *file = new QFile(filePath);
          file->open(QIODevice::ReadOnly);
      
          QHttpPart filePart;
          filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                             QVariant("form-data; name=\"uploadImage\"; filename="+file->fileName()));
          filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
          filePart.setBodyDevice(file);```
      
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      K 1 Reply Last reply
      0
      • dheerendraD dheerendra

        You are not setting the HTTP properties properly. Just copy the following code in your program. It should work. Also if something is problem, it should give network error.

            QFile *file = new QFile(filePath);
            file->open(QIODevice::ReadOnly);
        
            QHttpPart filePart;
            filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                               QVariant("form-data; name=\"uploadImage\"; filename="+file->fileName()));
            filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
            filePart.setBodyDevice(file);```
        
        
        K Offline
        K Offline
        Knox
        wrote on last edited by
        #3

        @dheerendra still to no avail, the server did not log any post and it did not show any error.

        Here is the new edited code

            qDebug()<<"Recieved in C ++ : " + path.replace("file:///","");
        
            QString string = path.replace("file:///","");
        
            QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
            multiPart->setBoundary("----WebKitFormBoundary7MA4YWxkTrZu0gW");
        
        //    QHttpPart imagePart;
        //    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data; boundary=" + multiPart->boundary()));
        //    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uploadImage\"; filename=\"" + string));
        //    QFile *file = new QFile(string);
        //    if (file->open(QIODevice::ReadOnly))
        //        qDebug() << "file opened";
        //    imagePart.setBodyDevice(file);
        
            QFile *file = new QFile(string);
            file->open(QIODevice::ReadOnly);
        
            QHttpPart filePart;
            filePart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"uploadImage\"; filename="+file->fileName()));
            filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
            filePart.setBodyDevice(file);
        
            file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
        
            multiPart->append(filePart);
        
        
        
            QUrl url("https://mighty-lionfish-83.localtunnel.me/imageUpload");
            QNetworkRequest request(url);
        
            QNetworkAccessManager manager;
            QNetworkReply *reply = manager.post(request, multiPart);
            multiPart->setParent(reply); // delete the multiPart with the reply
            // here connect signals etc.
        
        
        
            qDebug()<<"Function Finished";
        

        here is the Console output

        Starting C:\Qt_Projects\IMAGEviaXMLHTTP\debug\IMAGEviaXMLHTTP.exe...
        QML debugging is enabled. Only use this in a safe environment.
        Directory name "C:/Qt_Test" 
        
        qml: You chose: file:///C:/Qt_Projects/IMAGEviaXMLHTTP/Images/nfclogo.png
        qml: file:///C:/Qt_Projects/IMAGEviaXMLHTTP/Images/nfclogo.png
        "Recieved in C ++ : C:/Qt_Projects/IMAGEviaXMLHTTP/Images/nfclogo.png"
        Function Finished
        
        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Another thing I notice is that QNetworkAccessManager manager is created just before the post request. This indicates to me that you are not handling any signal like finished(..). So how to you know that response has come back ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          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