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

Unsure of how to configure QHttpMultiPart for Google Drive upload - Error 400



  • I am trying to upload a file to Google Drive using a multi part upload as described here https://developers.google.com/drive/api/v3/multipart-upload while following the Qt Documentation for QHttpMultiPart. I believe the error may be due to the headers. The API documentation states:

    Add the following top-level HTTP headers:
    Content-Type. Set to multipart/related, and include the boundary string you're using to identify the different parts of the request. For example: Content-Type: multipart/related; boundary=foo_bar_baz
    Content-Length. Set to the total number of bytes in the request body.

    Since QHttpMultiPart only supports Content-Type as a header Included these in my QNetworkRequest but I receive a 400 error that my request is not formatted properly. Is there some other way I should be setting my headers up? Or did I miss something when setting up QHttpMultiPart? Thanks.

       //get MIME type of file
    QMimeDatabase mimeDB; //contains a database of all MIME types
    QMimeType mime = mimeDB.mimeTypeForFile(filePath);
    QByteArray mimeType = mime.name().toUtf8();
    
    multiPart = new QHttpMultiPart(); //contains our multipart upload
    
    //part 1 - metadata
    QHttpPart metaData; 
    metaData.setRawHeader("Content-Type", "application/json; charset=UTF-8");
    QJsonObject metaBody; //body containing metadata
    QJsonValue JName(fileCopyHandler::extractName(filePath));
    QJsonValue JMime(mime.name());
    metaBody.insert("name", JName);
    metaBody.insert("mimeType", JMime);
    metaBody.insert("Description", "Uploaded by AutoBackup.");
    QByteArray metaBodyJson(QJsonDocument(metaBody).toJson());
    metaData.setBody(metaBodyJson);
    
    //part 2 - media data
    QHttpPart mediaData; 
    mediaData.setRawHeader("Content-Type", mimeType);
    QFile *file = new QFile(filePath);
    file->open(QIODevice::ReadOnly);
    mediaData.setBodyDevice(file);
    file->setParent(multiPart); //delete file when multiPart is deleted
    
    multiPart->append(metaData);
    multiPart->append(mediaData);
    QByteArray bodySize;
    bodySize.setNum(file->size() + metaBodyJson.size()); //number of bytes in entire request body
    
    //setup rest of the request
    QUrl uploadURL("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
    QNetworkRequest request(uploadURL);
    request.setRawHeader("Authorization", "Bearer " + authToken.toUtf8()); //convert authToken to QByteArray when we set header;
    request.setRawHeader("Content-Type", "multipart/related; boundary=" + multiPart->boundary());
    request.setRawHeader("Content-Length", bodySize);
    
    networkReply = networkManager->post(request, multiPart);
    
    QObject::connect(networkReply, SIGNAL(metaDataChanged()), this, SLOT(testSlot1()));

  • Lifetime Qt Champion

    Hi,

    One thing you can do is use something like Fiddler to check what you are sending to the google Drive API.



  • So I tried fiddler and wireshark and I am unable to see my POST request. I can see the request for receiving authentication however.


  • Lifetime Qt Champion

    Do you mean it's not sent ?


Log in to reply