Pregunta

I'm trying to use the REST Api to manage project roles in JIRA. I've been able to GET a list of the roles and "actors" and DELETE a role member. But I can't POST a new role member correctly. I keep getting a 400 or 405 error. I'm using HttpClient 4.3.2 and Jira 6.0.2. Here is my code:

// Set up ssl configuration as a user in JIRA instance

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://jira.install/rest/api/2/project/KEY/role/10000");

StringEntity input = new StringEntity("{\"group\":\"jira-users\" }");
input.setContentType("Application/json");
post.setEntity(input);

client.execute(post);

Has anyone been able to do a similar call successfully?

¿Fue útil?

Solución

The rest api requires basic authorization for a POST to the group roles but it's not explicitly stated in the API documentation. So here's how I got it to work:

String auth = "username:password";
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US_ASCII")));
String authHeader = "Basic " + new String(encodedAuth);

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
StringEntity input;

try {
    input = new StringEntity("{\"group\":\"your-jira-group\"}");
    input.setContentType("Application/json");
    post.setEntity(input);
    post.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    HttpResponse response = client.execute(post);
} catch (Exception ex) {
}

Two things were messing me up...the "A" in Application/json needed to be capitalized and you needed to authorize the session. I chose basic authentication and used HttpClient after testing it with curl.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top