Post Request Denied (405)

georgiossalon July 23, 2019

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());}}

 

 

 

2019-07-23 12_39_16-JiraProjects [C__Users_gsalonik_IdeaProjects_JiraProjectDataManagement] - ..._sr.png

1 answer

1 accepted

0 votes
Answer accepted
georgiossalon July 23, 2019

Found the error. The URL was wrong. It should be like the following:

https://example.net/jira/rest/api/2/version

Suggest an answer

Log in or Sign up to answer