QT upload (PUT) a file

Mac

I am using QT to upload a file to a web server. The web server accepts files using the following request:

curl -X POST -H 'Content-Type:multipart/form-data' 
-H 'Authorization: Token <token>' 
-F 'file=@file_to_upload.txt' 
https://some.web.site/api/v2/files/contents/

I am using roughly this QT calls to try to accomplish the same:

QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
QHttpPart filePart;

file.open(QIODevice::ReadOnly)

filePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data");
filePart.setBodyDevice(file);
multiPart.append(filePart);

QNetworkAccessManager mgr;
QNetworkRequest req(url);
req.setRawHeader("Authorization", ("Token <token>").data());

QNetworkReply * reply(mgr.put(req, &multiPart));

Right now this is what I get from the server:

File object is missing or invalid.

Can someone stop what the QT part is missing compared to the curl command? I would guess qt is missing some step that curl does behind the scenes. I would rather prefer a solution that does not involve me putting the whole request together manually.

Evgeny

You should make some code modifications:

file.open(QIODevice::ReadOnly);

//add next lines
filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/zip")); //or whatever type of your file.
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\""));
filePart.setHeader(QNetworkRequest::ContentLengthHeader, file.size());

//and your other code
filePart.setBodyDevice(file);
multiPart.append(filePart);

And also take attention that with curl you make POST request but with Qt - put. So also replace last line with this:

QNetworkReply * reply = mgr.post(req, &multiPart);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

QT upload (PUT) a file

From Dev

Use PUT to upload file with TIdHTTP

From Dev

File Upload via HTTP PUT Request

From Dev

Upload File Via PHP Curl PUT

From Dev

AJAX file upload to put data into data

From Dev

Upload File Via PHP Curl PUT

From Dev

PHP file upload PUT request alters file, while POST does not

From Dev

upload file to dropBox using /files_put javascript

From Dev

Upload text file to Google uploads via PHP FTP PUT

From Dev

Upload XML file, put in temporary folder or localStorage and access contents

From Dev

Trying to upload with a PUT request

From Dev

Put arguments in slots in Qt

From Dev

Fine Upload No File to upload

From Dev

Example of DropBox API PUT using Curl and Oauth 2 to Upload a file to DropBox

From Dev

Upload file using PUT verb in ASP.Net Web Api 2

From Dev

NSIS inetc::put, cannot upload file: "Try setting the Content-type header."

From Dev

NSIS inetc::put, cannot upload file: "Try setting the Content-type header."

From Dev

Upload TAR contained file to REST PUT API using PHP CURL without extracting

From Dev

Qt5 with CMake not compiling .ui file when related source files are put into a library

From Dev

QT-FTP Upload Error

From Dev

QT C++ - FTP Upload

From Dev

Angular file upload - intercept upload

From Dev

jQuery File Upload not triggering upload

From Dev

jsf upload file upload immediately

From Dev

What is the Difference between file_upload() and put_object() when uploading files to S3 using boto3

From Dev

PowerShell upload file to ftp will upload file twice

From Dev

cURL command to upload file does not upload file

From Dev

upload file with --upload-file to server

From Dev

PUT request for image upload not working in django rest

Related Related

HotTag

Archive