无法通过 rest api Java 将文件附加到 jira 中的问题

Unable to attach file to issue in jira via rest api Java

我想附加多个文件来发布。我能够成功创建问题,但是我在创建问题后附加文档时遇到问题。我已经提到这个 link SOLVED: attach a file using REST from scriptrunner 我收到 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 字段,而不是 post 内容。

我一直在使用 Rcarz's Jira client。它有点过时,但仍然有效。 也许查看它的代码会对你有所帮助,或者你可以直接使用它。 问题class:

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 中 class:

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",尝试添加它。