Error during multipart upload using Qt
-
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);
-
@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.
-
@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);
-
@daljit97
in this example you never add the created http-part to the multi-part object -
@raven-worx said:
@daljit97
in this example you never add the created http-part to the multi-part objectYeah, sorry I accidentally deleted it while deleting a comment.
-
@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?
-
@raven-worx the server is https://upload.pushbullet.com
-
@daljit97
so you receive the bad request error already from the "request itself" or in the Pushbullet's JSON response? -
@raven-worx The error is simple a reading of reply->errorString()
-
@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?
-
@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.
-
@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 ;) -
@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.
-
@daljit97
(i modified my answer while you responded in the meantime) -
@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.