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. Error during multipart upload using Qt
Forum Updated to NodeBB v4.3 + New Features

Error during multipart upload using Qt

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 2 Posters 2.9k 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.
  • D daljit97

    I am trying to upload a file using QNetworkAccessManager, but I always get an error (Error transferring url - server replied: Bad Request). Below is my code

    QString name = "Simple.txt";
    QString type = "text/plain; charset=utf-8";
    QString uploadUrl = "myuploadUrl";
    
    // setup the multipart request
    QString bound="---------------------------723690991551375881941828858";
    QByteArray data(QString("--"+bound+"\r\n").toLatin1());
    
    // write the file using standard method for multipart file upload
    data += "Content-Disposition: form-data; name=\"file\"; filename=\""+name.toLatin1()+"\"\r\n";
    data += "Content-Type: "+type.toLatin1()+"\r\n\r\n";
    
    data += "Hello, I am simple file";
    data += "\r\n";
    data += "--" + bound;
    
    qDebug() << data;
    // make the request with appropriate headers
    QNetworkRequest request(QUrl(uploadUrl));
    request.setRawHeader(QByteArray("Content-Type"),QString("multipart/form-data; boundary=" + bound).toLatin1());
    request.setRawHeader(QByteArray("Content-Length"), QString::number(data.length()).toLatin1());
    
    
    QNetworkReply *reply = networkManager->post(request,data);
    QObject::connect(reply, &QNetworkReply::finished, this, FileUploader::requestFinished);
    
    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on last edited by raven-worx
    #2

    @daljit97
    what may be the cause of the error is that the ending boundary is missing "--" at the end.

    Also note that there is QHttpMultiPart class available (since Qt4.8) which lets you send multi-part http requests in a convenient way.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    D 1 Reply Last reply
    1
    • raven-worxR raven-worx

      @daljit97
      what may be the cause of the error is that the ending boundary is missing "--" at the end.

      Also note that there is QHttpMultiPart class available (since Qt4.8) which lets you send multi-part http requests in a convenient way.

      D Offline
      D Offline
      daljit97
      wrote on last edited by daljit97
      #3

      @raven-worx said:

      @daljit97
      what may be the cause of the error is that the ending boundary is missing "--" at the end.

      Also note that there is QHttpMultiPart class available (since Qt4.8) which lets you send multi-part http requests in a convenient way.

      Yes, I have tried with QHttpMultipart too, but it gives the same result

              //setup the multipart request
              QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
              multiPart->setBoundary("---------------------------724690991551375881941828858");
      
              QHttpPart textPart;
              QString textString = "name =\"file\"; filename=\"" + name + "\"";
              textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(textString));
              textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(type));
              textPart.setBody("Hello, I am a simple file");
      
              multiPart->append(textPart);
      
              // make request
              QNetworkRequest request(uploadUrl);
              request.setHeader(QNetworkRequest::ContentTypeHeader,QString("multipart/form-data; boundary=" + multiPart->boundary()));
      
              QNetworkReply *reply  = networkManager->post(request, multiPart);
              multiPart->setParent(reply); // delete multiPart with reply
      
              QObject::connect(reply, &QNetworkReply::finished, this, FileUploader::requestFinished);
      
      raven-worxR 1 Reply Last reply
      0
      • D daljit97

        @raven-worx said:

        @daljit97
        what may be the cause of the error is that the ending boundary is missing "--" at the end.

        Also note that there is QHttpMultiPart class available (since Qt4.8) which lets you send multi-part http requests in a convenient way.

        Yes, I have tried with QHttpMultipart too, but it gives the same result

                //setup the multipart request
                QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
                multiPart->setBoundary("---------------------------724690991551375881941828858");
        
                QHttpPart textPart;
                QString textString = "name =\"file\"; filename=\"" + name + "\"";
                textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(textString));
                textPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(type));
                textPart.setBody("Hello, I am a simple file");
        
                multiPart->append(textPart);
        
                // make request
                QNetworkRequest request(uploadUrl);
                request.setHeader(QNetworkRequest::ContentTypeHeader,QString("multipart/form-data; boundary=" + multiPart->boundary()));
        
                QNetworkReply *reply  = networkManager->post(request, multiPart);
                multiPart->setParent(reply); // delete multiPart with reply
        
                QObject::connect(reply, &QNetworkReply::finished, this, FileUploader::requestFinished);
        
        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #4

        @daljit97
        in this example you never add the created http-part to the multi-part object

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        D 1 Reply Last reply
        0
        • raven-worxR raven-worx

          @daljit97
          in this example you never add the created http-part to the multi-part object

          D Offline
          D Offline
          daljit97
          wrote on last edited by
          #5

          @raven-worx said:

          @daljit97
          in this example you never add the created http-part to the multi-part object

          Yeah, sorry I accidentally deleted it while deleting a comment.

          raven-worxR 1 Reply Last reply
          0
          • D daljit97

            @raven-worx said:

            @daljit97
            in this example you never add the created http-part to the multi-part object

            Yeah, sorry I accidentally deleted it while deleting a comment.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #6

            @daljit97
            no need to set the header again on the request. This is already implied by using QMultiPartHttp class.

            Beside that, what is the server you are uploading to?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            D 1 Reply Last reply
            0
            • raven-worxR raven-worx

              @daljit97
              no need to set the header again on the request. This is already implied by using QMultiPartHttp class.

              Beside that, what is the server you are uploading to?

              D Offline
              D Offline
              daljit97
              wrote on last edited by
              #7

              @raven-worx the server is https://upload.pushbullet.com

              raven-worxR 1 Reply Last reply
              0
              • D daljit97

                @raven-worx the server is https://upload.pushbullet.com

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #8

                @daljit97
                so you receive the bad request error already from the "request itself" or in the Pushbullet's JSON response?

                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                If you have a question please use the forum so others can benefit from the solution in the future

                D 1 Reply Last reply
                0
                • raven-worxR raven-worx

                  @daljit97
                  so you receive the bad request error already from the "request itself" or in the Pushbullet's JSON response?

                  D Offline
                  D Offline
                  daljit97
                  wrote on last edited by
                  #9

                  @raven-worx The error is simple a reading of reply->errorString()

                  raven-worxR 1 Reply Last reply
                  0
                  • D daljit97

                    @raven-worx The error is simple a reading of reply->errorString()

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #10

                    @daljit97
                    ok, also post the contents of the reply (even if it has an error set).
                    Maybe the pushbullet server has some more hints there.

                    Also isn't there any authentication required by the server?

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    D 1 Reply Last reply
                    0
                    • raven-worxR raven-worx

                      @daljit97
                      ok, also post the contents of the reply (even if it has an error set).
                      Maybe the pushbullet server has some more hints there.

                      Also isn't there any authentication required by the server?

                      D Offline
                      D Offline
                      daljit97
                      wrote on last edited by
                      #11

                      @raven-worx said:

                      @daljit97
                      ok, also post the contents of the reply (even if it has an error set).
                      Maybe the pushbullet server has some more hints there.

                      Also isn't there any authentication required by the server?

                      This is the reply from the server

                      {\"error\":{\"code\":\"invalid_request\",\"type\":\"invalid_request\",\"message\":\"No file included in request.\",\"cat\":\"(=\xEF\xBC\xB4\xE3\x82\xA7\xEF\xBC\xB4=)\"},\"error_code\":\"invalid_request\"}'
                      

                      As for the authentication it shouldn't be required but I performed a check with a token and it still gives the same error.

                      raven-worxR 1 Reply Last reply
                      0
                      • D daljit97

                        @raven-worx said:

                        @daljit97
                        ok, also post the contents of the reply (even if it has an error set).
                        Maybe the pushbullet server has some more hints there.

                        Also isn't there any authentication required by the server?

                        This is the reply from the server

                        {\"error\":{\"code\":\"invalid_request\",\"type\":\"invalid_request\",\"message\":\"No file included in request.\",\"cat\":\"(=\xEF\xBC\xB4\xE3\x82\xA7\xEF\xBC\xB4=)\"},\"error_code\":\"invalid_request\"}'
                        

                        As for the authentication it shouldn't be required but I performed a check with a token and it still gives the same error.

                        raven-worxR Offline
                        raven-worxR Offline
                        raven-worx
                        Moderators
                        wrote on last edited by raven-worx
                        #12

                        @daljit97
                        there you have the message "No file included in request". This should already help alot and means that the error is Pushbullet specific and actually has nothing todo with Qt itself.

                        I think you are just missing a parameter which is expected by the pushbullet server. So you need to research how the request is expected.

                        Pushbullet docs say:

                        Copy of all the parameters from the data object in the response to the upload request. In addition to that, the file should be uploaded as the parameter file.
                        

                        I think i've also read that you need to add the "file_type" and "file_name" parameters to the request. Maybe this already solves it.

                        Next time please provide all the relevant info from the beginning.
                        The type of the server is an essential information during upload ;)

                        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                        If you have a question please use the forum so others can benefit from the solution in the future

                        D 1 Reply Last reply
                        0
                        • raven-worxR raven-worx

                          @daljit97
                          there you have the message "No file included in request". This should already help alot and means that the error is Pushbullet specific and actually has nothing todo with Qt itself.

                          I think you are just missing a parameter which is expected by the pushbullet server. So you need to research how the request is expected.

                          Pushbullet docs say:

                          Copy of all the parameters from the data object in the response to the upload request. In addition to that, the file should be uploaded as the parameter file.
                          

                          I think i've also read that you need to add the "file_type" and "file_name" parameters to the request. Maybe this already solves it.

                          Next time please provide all the relevant info from the beginning.
                          The type of the server is an essential information during upload ;)

                          D Offline
                          D Offline
                          daljit97
                          wrote on last edited by
                          #13

                          @raven-worx Alright thank you, I'll keep that in mind. However, I do not understand why it gives that error, since I am attaching the file in the request.

                          raven-worxR 1 Reply Last reply
                          0
                          • D daljit97

                            @raven-worx Alright thank you, I'll keep that in mind. However, I do not understand why it gives that error, since I am attaching the file in the request.

                            raven-worxR Offline
                            raven-worxR Offline
                            raven-worx
                            Moderators
                            wrote on last edited by
                            #14

                            @daljit97
                            (i modified my answer while you responded in the meantime)

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            D 1 Reply Last reply
                            0
                            • raven-worxR raven-worx

                              @daljit97
                              (i modified my answer while you responded in the meantime)

                              D Offline
                              D Offline
                              daljit97
                              wrote on last edited by
                              #15

                              @raven-worx Thank you very much for your help. I have added the missing "--" at the end of the request (I forgot to clean the project, that's probably why it didn't work before). I did not use the data parameter since it was deprecated by Pushbullet.

                              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