c++ - Adding attachment to JIRA issue using the REST API and Qt with QNetworkRequest -
i'm trying add attacment existing jira issue using rest api , qt.
when run code below, reply empty array ("[]").
edit: updated code
qstring apihandler::attachfile(qstring fileloc, qstring issuekey, qstring cookie) { //create multipart qhttpmultipart *multipart = new qhttpmultipart(qhttpmultipart::formdatatype); qfile *file = new qfile(fileloc); //create httppart 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()); // 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 using jira rest api documentation , qt documentation reference, , looking off of this java implementation (which i've tried replicate).
it seems i'm either missing header, or adding file incorrectly.
any appreciated!
edit - here's part of wireshark comparing example api using curl (left) , , code (right). 1 on left works, , has different mime data, i'm not sure how implement in qt
okay, figured out. might 1 on earth using (or use) qt interact jira api, posterity, here's came with:
qstring apihandler::attachfile(qstring fileloc, qstring issuekey, qstring cookie) { qhttpmultipart *multipart = new qhttpmultipart(qhttpmultipart::formdatatype); qhttppart filepart; qfileinfo fileinfo(fileloc); //what 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()); // 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 reply qbytearray bytes=reply->readall(); //return reply json return qstring::fromutf8(bytes.data(), bytes.size()); delete file; delete multipart; delete reply; delete mngr; }
the key part here, , doing wrong, way in set boundary multipart. instead of setting in header, should have used:
multipart->setboundary()
which can see reflected above.
if you're coming across , going use it, i'd recommend cleaning bit, first. works!
Comments
Post a Comment