Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. sending form-data with qml
Forum Updated to NodeBB v4.3 + New Features

sending form-data with qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 3 Posters 2.1k 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.
  • E Offline
    E Offline
    echo55
    wrote on 11 Nov 2018, 10:51 last edited by
    #1

    Hi,

    I can't make it works.
    I just want to send a file using form-data format to an url of an API i did.
    I'm using a fileDialog to choose the file , then take the filepath and use it for the form

    I checked httpmultipart , i tried to do it in js directly but new FormData() isn't recognized and it seems that it's normal because qml doesn't use the server side of js (if i understood well).

    I tried using boundaries because some people said that it was the problem but it doesn't work too.

    here is my code :

    the file Dialog :

      FileDialog {
            id: fileDialog
            title: "Please choose a file"
            folder: shortcuts.home
            onAccepted: {
                console.log("You chose: " + fileDialog.fileUrl)
                send1(fileDialog.fileUrl)
                fileDialog.close()
            }
            onRejected: {
                console.log("Canceled")
                fileDialog.close()
            }
            Component.onCompleted: visible = false
        }
    

    the function send1 goes at the end to (and it's here the problem) :

    
    int sendRequest::sendfile(QString tok, QString imageFilename)
    {
    
        imageFilename = imageFilename.mid(7, -1);
        QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        multiPart->setBoundary("---------------------jasglfuyqwreltjaslgjlkdaghflsdgh");
        
        
        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(imageFilename);
        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(myapiUrl);
        QNetworkRequest request(url);
        
        QNetworkAccessManager manager;
        request.setRawHeader("Authorization", tok.toLatin1());
        QNetworkReply *reply = manager.post(request, multiPart);
        multiPart->setParent(reply); // delete the multiPart with the reply
        // here connect signals etc.
    }
    

    I'm getting this error , sometimes with a problem of pointer.

    1:43:45: The program has unexpectedly finished.
    11:43:45: The process was ended forcefully.
    

    sometimes this

    double free or corruption (!prev)
    

    Thank you.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 11 Nov 2018, 12:33 last edited by
      #2

      I'm not sure to which url you are trying to send the image. In order to trouble shoot there is software called postman. Use the software to upload the image. It give you the all the http header options. Based on this header work the parameters to set for the header.

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

      1 Reply Last reply
      0
      • E Offline
        E Offline
        echo55
        wrote on 11 Nov 2018, 13:10 last edited by
        #3

        I didnt put it for security reason .
        and i know postman , it works on Postman but there is no export for qt / c++ / qml .
        Now it doesn't stop anymore but nothing happened
        i used a signal to connect and see if it's over but it's not triggered

        connect(reply, &QNetworkReply::finished, this, &sendRequest::uploadImageFinished);
        
        void sendRequest::uploadImageFinished() {
        qDebug() << "tesst";
        QString xmlReply = QString(reply->readAll());
        delete reply;
        qDebug() << xmlReply;
        }
        
        

        Nothing , not even the "tesst"

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 12 Nov 2018, 14:59 last edited by
          #4

          The reason I asked for postman is that, from post man you should be able to see how the parameters are passed. Generally it will be issue with parameter passing when you are uploading the file in multipart. Your web server may be expecting 'fileName' argument & you may be sending 'name' argument. Please check this.

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

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 12 Nov 2018, 19:52 last edited by
            #5

            Hi,

            You should rather use reply->deleteLater(); and let the event loop destroy that stuff.

            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
            1
            • E Offline
              E Offline
              echo55
              wrote on 13 Nov 2018, 10:16 last edited by
              #6

              reply->deleteLayer() where ?

              here is the export from Postman in libcurl :

              CURL *hnd = curl_easy_init();
              
              curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
              curl_easy_setopt(hnd, CURLOPT_URL, "theurl , i dont put it for security");
              
              struct curl_slist *headers = NULL;
              headers = curl_slist_append(headers, "Postman-Token: 0d096d09-5558-4d97-8b83-f64ded76c724");
              headers = curl_slist_append(headers, "cache-control: no-cache");
              headers = curl_slist_append(headers, "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImJ1bmRsZXMiOlt7Imluc3RydW1lbnRzIjpbeyJfaWQiOiI1YmUwMjBhY2RiNWYzMjZjMDljNjhlNWIiLCJuYW1lIjoiQmF0dGVyaWUiLCJtaWRpQ29kZSI6MSwiaXNEcnVtIjp0cnVlLCJfX3YiOjAsImltYWdlIjoiaHR0cDovL2FwaS5iYWNraW5ndHJhY2tsaXZlLmNvbTozMDAwL3B1YmxpYy9pbnN0cnVtZW50c0ltYWdlcy9teUltYWdlLmU3MzA5ZGIwMThjNzBhNTZiNmExZmUiLCJ1c2VybmFtZSI6ImZhYmllbmciLCJlbWFpbCI6ImZhYmllbi5nYWRldEBlcGl0ZWNoLmV1IiwibGFzdE5hbWUiOiJnIiwiZmlyc3ROYW1lIjoiZmFiaWVuIiwicGFzc3dvcmQiOiIkMmEkMTAkUkNsWnV1MXE5a0w1QlAzTFo3QjBhdXluQ0lxNWxRRUZoYXBOVXRneC45OFlNVFhLVk9QTHEiLCJhZGRyZXNzIjoiIiwiemlwQ29kZSI6IiIsImNvdW50cnkiOiIiLCJjaXR5IjoiIiwicGhvbmUiOiIiLCJfX3YiOjB9LCJwZXJtaXNzaW9ucyI6WyJVU0VSIl0sImlhdCI6MTU0MTg3NzkyMiwiZXhwIjoxNTQyNDgyNzIyfQ.28yH7D9Lmb4bg21bSt9tqbD2ic8AusjTrNoOxvf8yxk");
              headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
              headers = curl_slist_append(headers, "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
              curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
              
              curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"myImage\"; filename=\"jam2.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
              
              CURLcode ret = curl_easy_perform(hnd);
              

              This works if i use PostMan.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                echo55
                wrote on 13 Nov 2018, 15:47 last edited by
                #7

                No one ? = (

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 13 Nov 2018, 16:35 last edited by
                  #8

                  Try

                  QHttpPart filePart;
                  filePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                  QVariant("form-data; name="myImage"; filename="jam2.png"));

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

                  1 Reply Last reply
                  0

                  1/8

                  11 Nov 2018, 10:51

                  • Login

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