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

Trouble posting to a resful API



  • 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


  • Qt Champions 2017

    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 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
    

  • Qt Champions 2017

    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 ?