0

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);

However running this python script, which should do the same thing, works.

import requests
import json

file_name = "Simple.txt"
uploadUrl = "myUploadUrl";
resp = requests.post(uploadUrl, data=r["data"], files={"file": open(file_name, "rb")})
print (resp);
4

2 回答 2

0

不要手动创建请求,而是使用 QT 内部。在这里你可以试试这个对我有用的代码。

void OfflineLogHandler::uploadOfflineLogFile(QString filePath){

    QUrl targateUlr(APIUrls::getInstance()->getFlightLogUploadURL());  //pass URL here
    APIUrls *apiUrls = APIUrls::getInstance();
    qDebug()<<"Target upload offline log url:"<<targateUlr;
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

    QHttpPart formHeader;
    formHeader.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"\"; filename=\""+filePath.section("/",-1)+"\""));
    if(filePath.contains(".json"))
        formHeader.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    else if(filePath.contains(".csv"))
        formHeader.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/csv"));
    QFile *file = new QFile(filePath);
    file->open(QIODevice::ReadOnly);
    formHeader.setBodyDevice(file);

    multiPart->append(formHeader);
    file->setParent(multiPart);

    QNetworkRequest request;
    request.setUrl(targateUlr);

    QString CredData = apiUrls->getCredentials();
    QString headerData = apiUrls->getApiAuthorizationType() + CredData;
    qDebug()<<"access:"<<headerData.toLocal8Bit();
    request.setRawHeader( "Authorization", headerData.toLocal8Bit() );
    request.setRawHeader("CustomeData1","Data1");
    request.setRawHeader("CustomeData2","Data2");
    request.setHeader(QNetworkRequest::ContentTypeHeader,QString("multipart/form-data; boundary="+multiPart->boundary()).toLatin1());

    QNetworkAccessManager *restclient; //in class
    restclient = new QNetworkAccessManager(); //constructor

    disconnect(restclient,SIGNAL(finished(QNetworkReply*)), this, nullptr);
    connect(restclient, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleFileUploadResponse(QNetworkReply*)));
    networkReply = restclient->post(request,multiPart);
    connect(networkReply,SIGNAL(uploadProgress(qint64, qint64)),this,SLOT(fileUprogress(qint64, qint64)));
    qDebug()<<"net:"<<networkReply;
}
于 2021-12-31T11:14:33.353 回答
0

data += "\r\n"通过在数据末尾添加 a 解决了该问题。

于 2016-07-28T11:09:45.647 回答