Creating a new JIRA component using REST API

I’m trying to add a new component in JIRA using the REST API. I found the POST endpoint /rest/api/2/component but I’m not sure about the required input parameters. Here’s my current code:

HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("http://jira-server:8080/rest/api/2/component/");

String auth = Base64.getEncoder().encodeToString("username:password".getBytes());
request.setHeader("Authorization", "Basic " + auth);

String jsonBody = "{\"name\":\"New Component\",\"description\":\"Test component\",\"leadUserName\":\"john\",\"assigneeType\":\"PROJECT_LEAD\",\"project\":\"TESTPROJ\"}";
StringEntity entity = new StringEntity(jsonBody);
entity.setContentType("application/json");
request.setEntity(entity);

HttpResponse response = client.execute(request);

When I run this, I get a 400 Bad Request error. Can someone help me figure out what I’m doing wrong? Thanks!

I’ve dealt with this issue before, and I can share some insights that might help. The problem likely lies in how you’re formatting the JSON body. JIRA’s REST API can be quite particular about the structure.

Make sure your project key is correct as it appears in JIRA. The ‘leadUserName’ field is optional, so if it’s causing issues, consider omitting it. Also, double-check that ‘assigneeType’ is a valid value. While ‘PROJECT_LEAD’ should work, you might try ‘PROJECT_DEFAULT’ if you continue encountering problems.

Using a JSON library like Gson or Jackson can help you avoid formatting mistakes. Finally, check the detailed response body because JIRA often includes helpful error messages when you receive a 400 error.

hey tom, i’ve run into this too. make sure ur JSON is spot on. try using a JSON validator online to check. also, double check ur project key - that’s usually the culprit. if all else fails, hit up JIRA’s API docs. they’ve got examples that might help u out. good luck!

I’ve encountered similar issues when working with JIRA’s REST API. One thing that’s often overlooked is the content type header. Try adding this line before executing the request:

request.setHeader(“Content-Type”, “application/json”);

Also, ensure your JIRA instance is up-to-date and supports the API version you’re using. Sometimes, older versions have quirks with certain endpoints.

Another tip: use a tool like Postman to test the API call first. It can help isolate whether the problem is in your Java code or the actual API request. If it works in Postman but not in your code, you might have a serialization issue.

Lastly, double-check your authentication. Some JIRA setups require additional permissions for creating components. Make sure your user has the necessary rights.