Upload files to JIRA via REST API

We all know very well that the request and response format for the JIRA REST API is represented as JSON. I successfully extracted the attachment details of the downloaded files using a URL like http://example.com:8080/jira/rest/api/2/attachment .

Now I need to work with uploading files to JIRA using the same REST API. I have a java client and I pointed out that I need to send multi-page input using MultiPartEntity . I don't know how to send the X-Atlassian-Token: nocheck with a JSON request. When searching for documents, I received only examples of curl-based queries. Can someone help me fix this?

0
source share
1 answer

I did it like this and it works:

 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 ); } } 
+1
source

Source: https://habr.com/ru/post/922474/


All Articles