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. Unsure of how to configure QHttpMultiPart for Google Drive upload - Error 400

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 652 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.
  • C Offline
    C Offline
    cjrennie
    wrote on last edited by
    #1

    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()));
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      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
      2
      • C Offline
        C Offline
        cjrennie
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Do you mean it's not sent ?

          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
          0

          • Login

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