通过REST API在JIRA中上传文件 [英] upload files in JIRA via REST API
问题描述
我们都非常清楚JIRA REST API的请求和响应格式都是JSON形式.我使用http://example.com:8080/jira/rest/api/2/attachment
类型的url成功检索了上载文件的附件详细信息.
We all pretty well know that the request and response format for JIRA REST API are in the form of JSON. I successfully retrieved the attachment details of the uploaded files using the url of type http://example.com:8080/jira/rest/api/2/attachment
.
我现在需要使用相同的REST API将文件上传到JIRA.我拥有一个Java客户端,并且其声明的内容需要使用MultiPartEntity
发布多部分输入.我不知道如何使用JSON请求提交X-Atlassian-Token: nocheck
的标头.搜索文档时,我只有基于curl的请求示例.谁能帮我解决这个问题?
I now need to work on file upload on to JIRA using the same REST API. I own a java client and its stated tat I need to post multipart input using MultiPartEntity
. I do not know how to submit a header of X-Atlassian-Token: nocheck
with the JSON request. Searching the documents I got only curl based request examples. Can anyone help me fixing this?
推荐答案
这就是我依赖okhttp和okio的方式
This is how i did it dependencies okhttp and okio
private static void upload(File file) throws Exception{
final String address = "https://domain/rest/api/2/issue/issueId/attachments";
final OkHttpClient okHttpClient = new OkHttpClient();
final RequestBody formBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", file.getName(),
RequestBody.create(MediaType.parse("text/plain"), file))
.build();
final Request request = new Request.Builder().url(address).post(formBody)
.addHeader("X-Atlassian-Token", "no-check")
.addHeader("Authorization", "Basic api_token_from_your_account")
.build();
final Response response = okHttpClient.newCall(request).execute();
System.out.println(response.code() + " => " + response.body().string());
}
这篇关于通过REST API在JIRA中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!