Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How modify fields in JIRA with HTTP request in HttpURLConnection?

Amine Chefiri April 20, 2016

I tried with the following example but it does not work sad

public static void main(String[] args) {
        
		String data = "{\"fields\":{\"customfield_10005\":\"test\"}}";

  		this.authenticate();
  
 		URL url = new URL("http://jira.sylob.local/rest/api/2/issue/105868?  fields=customfield_10005");
            
  		HttpURLConnection connection =  this.httpGet(url.toString(), this.authCookie);
		
		OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
            osw.write(data);
            osw.flush();
}
 
private void authenticate() throws Exception {
        StringBuilder authUrl = new StringBuilder("http://").append("jira.sylob.local").append("/rest/auth/1")
                .append("/session").append("?os_username=")
                .append(URLEncoder.encode("support", GeneralStatic.UTF8)).append("&os_password=")
                .append(URLEncoder.encode("support81", GeneralStatic.UTF8));
        HttpURLConnection connection =
            (HttpURLConnection) new URL(authUrl.toString()).openConnection();
        String cookie = "";
        mainLoop: for (Entry<String, List<String>> headers : connection.getHeaderFields()
            .entrySet()) {
            if (StringUtils.equals(headers.getKey(), "Set-Cookie")) {
                for (String cookieTmp : headers.getValue()) {
                    if (StringUtils.containsIgnoreCase(cookieTmp, "JSESSION")) {
                        cookie = cookieTmp;
                        break mainLoop;
                    }
                }
            }
        }
        if (StringUtils.isEmpty(cookie)) {
            throw new StandardCochiseMetierException(new CochiseMessage(
                "importTempsJIRA.exception.authentification"));
        }
        this.authCookie = cookie;
    }

private static HttpURLConnection httpGet(String url, String cookie) throws Exception {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestMethod("PUT");
        connection.setDoInput(true);
        connection.setDoOutput(true);
        if (StringUtils.isNotEmpty(cookie)) {
            connection.setRequestProperty("Cookie", cookie);
        }
        connection.connect();
        return connection;
    }

Please Can you help me

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 29, 2016

Whan namely does not work? Do you get any mistake or responce for this?

TAGS
AUG Leaders

Atlassian Community Events