[英]Unable to attach file to issue in jira via rest api Java
我想附加多個文件來發布。 我能夠成功創建問題,但是在創建問題后附加文檔時遇到問題。 我已經提到此鏈接已解決:使用腳本運行器中的 REST 附加文件我收到 404 錯誤,即使問題存在並且用戶也擁有所有權限。
File fileToUpload = new File("D:\\dummy.txt");
InputStream in = null;
try {
in = new FileInputStream(fileToUpload);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
HttpResponse < String > response3 = Unirest
.post("https://.../rest/api/2/issue/test-85/attachments")
.basicAuth(username, password).field("file", in , "dummy.txt")
.asString();
System.out.println(response3.getStatus());
這里test-85是一個 issueKey 值。 我正在使用open-unirest-java-3.3.06.jar 。 我附加文件的方式是否正確?
我不確定 open-unirest 如何管理它的字段,也許它試圖將它們作為 json 字段,而不是發布內容。
我一直在使用Rcarz 的 Jira 客戶端。 它有點過時了,但它仍然有效。 也許查看它的代碼會對您有所幫助,或者您可以直接使用它。 問題類:
public JSON addAttachment(File file) throws JiraException {
try {
return restclient.post(getRestUri(key) + "/attachments", file);
} catch (Exception ex) {
throw new JiraException("Failed add attachment to issue " + key, ex);
}
}
在 RestClient 類中:
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
public JSON post(String path, File file) throws RestException, IOException, URISyntaxException {
return request(new HttpPost(buildURI(path)), file);
}
private JSON request(HttpEntityEnclosingRequestBase req, File file) throws RestException, IOException {
if (file != null) {
File fileUpload = file;
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
ent.addPart("file", new FileBody(fileUpload));
req.setEntity(ent);
}
return request(req);
}
所以我不知道你為什么會得到 404,Jira 有時很模糊並且不太清楚它的錯誤,嘗試打印完整的錯誤,或者如果可以的話檢查 Jira 的日志。 也許它只是"X-Atlassian-Token", "nocheck"
,嘗試添加它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.