Json 通过RESTAPI上传JIRA中的文件
我们都很清楚,JIRA REST API的请求和响应格式是JSON格式的。我使用Json 通过RESTAPI上传JIRA中的文件,json,rest,attachment,jira,Json,Rest,Attachment,Jira,我们都很清楚,JIRA REST API的请求和响应格式是JSON格式的。我使用类型的url成功检索了上载文件的附件详细信息http://example.com:8080/jira/rest/api/2/attachment 我现在需要使用相同的RESTAPI将文件上传到JIRA。我拥有一个java客户机及其声明的tat,我需要使用MultiPartEntity发布多部分输入。我不知道如何在JSON请求中提交X-Atlassian-Token:nocheck的头。搜索文档时,我只得到了基于cur
类型的url成功检索了上载文件的附件详细信息http://example.com:8080/jira/rest/api/2/attachment
我现在需要使用相同的RESTAPI将文件上传到JIRA。我拥有一个java客户机及其声明的tat,我需要使用
MultiPartEntity
发布多部分输入。我不知道如何在JSON请求中提交X-Atlassian-Token:nocheck
的头。搜索文档时,我只得到了基于curl的请求示例。有人能帮我修一下吗?我是这样做的,而且效果很好:
public static void main( String[] args ) throws Exception {
File f = new File(args[ 0 ]);
String fileName = f.getName();
String url = "https://[JIRA-SERVER]/rest/api/2/issue/[JIRA-KEY]/attachments";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost( url );
post.setHeader( "Authorization", basicAuthHeader( "username", "password" ) );
post.setHeader( "X-Atlassian-Token", "nocheck" );
HttpEntity reqEntity = MultipartEntityBuilder.create()
.setMode( HttpMultipartMode.BROWSER_COMPATIBLE )
.addBinaryBody( "file",
new FileInputStream( f ),
ContentType.APPLICATION_OCTET_STREAM,
f.getName() )
.build();
post.setEntity( reqEntity );
post.setHeader( reqEntity.getContentType() );
CloseableHttpResponse response = httpClient.execute( post );
}
public static String basicAuthHeader( String user, String pass ) {
if ( user == null || pass == null ) return null;
try {
byte[] bytes = ( user + ":" + pass ).getBytes( "UTF-8" );
String base64 = DatatypeConverter.printBase64Binary( bytes );
return "Basic " + base64;
}
catch ( IOException ioe ) {
throw new RuntimeException( "Stop the world, Java broken: " + ioe, ioe );
}
}
我就是这样做的,okhttp和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());
}
可能是我开始时的副本,它有一个testAddAttachment方法感谢Mdoar。但是仍然没有关于如何发布多党派的提示。