我正在尝试使用REST API和Qt为现有的JIRA问题添加一个attacment。
当我运行下面的代码时,回复是一个空数组(“[]”)。
编辑:更新了代码
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和qt documentation以供参考,并查看this java实现(我试图复制)。
好像我要么错过了标题,要么错误地添加了文件。
非常感谢任何帮助!
编辑 - 这是wirehark的一部分,比较了使用curl(LEFT)的api和我的代码(RIGHT)的示例。左边的那个工作,显然有不同的MIME数据,但我不确定如何在Qt中实现
答案 0 :(得分:0)
好的,所以我明白了。我可能是世界上唯一一个正在使用(或将使用)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;
}
这里的关键部分,以及我做错了,是我为多部分设置边界的方式。我应该使用:
而不是在标题中设置它 multipart->setBoundary()
您可以在上面看到。
如果您遇到此问题并准备使用它,我建议您先清理一下。但它有效!