使用 REST API 和 Qt with QNetworkRequest 添加附件到 JIRA 问题

Adding attachment to JIRA issue using the REST API and Qt with QNetworkRequest

我正在尝试使用 REST API 和 Qt 向现有的 JIRA 问题添加附件。

当我运行下面的代码时,回复是一个空数组(“[]”)。

编辑:更新了代码

QString APIhandler::attachFile(QString fileloc, QString issueKey, QString cookie)
{

//create multiPart
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QFile *file = new QFile(fileloc);
//create httpPart for file
QHttpPart filePart;

filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+ file->fileName()+ "\""));



file->open(QIODevice::ReadOnly); 
filePart.setBodyDevice(file);

file->setParent(multiPart);

multiPart->append(filePart);

QNetworkAccessManager *mngr = new QNetworkAccessManager();

QUrl issurl(baseURL + "/api/2/issue/"+ issueKey + "/attachments");

QNetworkRequest req(issurl);
QNetworkReply *reply ;

QEventLoop loop;

//add headers
req.setRawHeader("cookie", "JSESSIONID = " + cookie.toUtf8()); // the session cookie
req.setRawHeader("X-Atlassian-Token", "nocheck");
req.setRawHeader("Content-Type", "multipart/form-data; boundary=------------------------53a5a2cd1d9c8b7f");
//req.setRawHeader("Content-Length", postDataSize);

reply = mngr->post(req, multiPart);
multiPart->setParent(reply);
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));

loop.exec();


return reply->readAll();

}

I am using the JIRA REST API documentation and qt documentation for reference, and looking off of this java 实现(我试图复制)。

我似乎缺少 header,或者添加的文件不正确。

非常感谢任何帮助!

编辑 - 这是 wireshark 的一部分,比较了 api 使用 curl(左)和我的代码(右)的示例。左边的有效,显然有不同的 MIME 数据,但我不确定如何在 Qt

中实现它

好的,我明白了。我可能是地球上唯一一个正在使用(或将使用)Qt 与 JIRA 交互的人 API,但对于后代来说,这是我想出的:

QString APIhandler::attachFile(QString fileloc, QString issueKey, QString cookie)
{

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart filePart;
    QFileInfo fileInfo(fileloc);

    //what I wasn't doing before!
    multiPart->setBoundary("------------------------53a5a2cf4d9c8b7f");

    filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+fileInfo.fileName() +"\""));
    filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/octet-stream"));

    QFile *file = new QFile(fileloc);
    file->open(QIODevice::ReadOnly); 
    filePart.setBodyDevice(file);

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

    QNetworkAccessManager *mngr = new QNetworkAccessManager();

    QUrl issurl(baseURL + "/api/2/issue/"+ issueKey + "/attachments");

    QNetworkRequest req(issurl);
    QNetworkReply *reply ;

    QEventLoop loop;

    //add headers
    req.setRawHeader("X-Atlassian-Token", "nocheck");
    req.setRawHeader("cookie", "JSESSIONID = " + cookie.toUtf8()); // the session cookie
    req.setRawHeader("Content-Type", "multipart/form-data;boundary=------------------------53a5a2cf4d9c8b7f");

    reply = mngr->post(req, multiPart);
    multiPart->setParent(reply);
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));

    loop.exec();

    //read the reply
    QByteArray bytes=reply->readAll();

    //return the reply JSON 
    return QString::fromUtf8(bytes.data(), bytes.size());

    delete file;
    delete multiPart;
    delete reply;
    delete mngr;

}

这里的关键部分,也是我做错的地方,是我为多部分设置边界的方式。而不是在 header 中设置它,我应该使用:

multipart->setBoundary()

你可以在上面看到。

如果您遇到这个问题并打算使用它,我建议您先稍微清理一下。但它有效!