I am trying to post a Version in Jira through Java and the REST API.
Unfortunately, I am getting a 405 (request denied). I do know that I am able to use GET and I know that it is possible to also POST. I am under the impression that the problem lies in the libraries making the request.
A college that used that same username and password managed to POST. He used Groovy and in addition other libraries.
In the following my code:
import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.node.JsonNodeFactory;import com.fasterxml.jackson.databind.node.ObjectNode;import com.mashape.unirest.http.HttpResponse;import com.mashape.unirest.http.JsonNode;import com.mashape.unirest.http.ObjectMapper;import com.mashape.unirest.http.Unirest;import com.mashape.unirest.http.exceptions.UnirestException;
import java.io.IOException;
public class testPost {
public static void main (String[] args) {
// The payload definition using the Jackson library
JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
{ payload.put("description", "An excellent version.");
payload.put("name", "New Version 1");
payload.put("releaseDate", "2010-07-06");
payload.put("project","KEY"); }
// Connect Jackson ObjectMapper to Unirest
Unirest.setObjectMapper(new ObjectMapper() {
private com.fasterxml.jackson.databind.ObjectMapper jacksonObjectMapper
= new com.fasterxml.jackson.databind.ObjectMapper();
public <T> T readValue(String value, Class<T> valueType) {
try {
return jacksonObjectMapper.readValue(value, valueType);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String writeValue(Object value) {
try { return jacksonObjectMapper.writeValueAsString(value);
} catch (JsonProcessingException e) {
e.printStackTrace(); } return "";
}
});
// This code sample uses the 'Unirest' library:
// http://unirest.io/java.html
HttpResponse<JsonNode> response = null;
try {
response = Unirest.post("https://example.net/jira/rest/api/2/project/KEY/versions")
.basicAuth("username", "password")
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.body(payload)
.asJson();
} catch ( UnirestException e) {
e.printStackTrace();
}
System.out.println(response.getBody());}}
Found the error. The URL was wrong. It should be like the following:
https://example.net/jira/rest/api/2/version
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.