sending form-data with qml



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


  • Qt Champions 2017

    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.



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


  • Qt Champions 2017

    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.


  • Lifetime Qt Champion

    Hi,

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



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



  • No one ? = (


  • Qt Champions 2017

    Try

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.