I need to create a component in JIRA using the REST API endpoint. I’m using the POST /rest/api/2/component endpoint but getting HTTP 400 error. Not sure what parameters I should pass or if my JSON format is correct.
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://localhost:8080/rest/api/2/component/");
String auth = AuthHelper.getAuthorizationHeader();
request.setHeader("Authorization", auth);
StringEntity payload = new StringEntity("\"name\":\"TestComponent\",\"description\":\"Sample component for testing\",\"leadUserName\":\"admin\",\"assigneeType\":\"PROJECT_LEAD\",\"isAssigneeTypeValid\":\"false\",\"project\":\"DEMO\");
payload.setContentType("application/json");
request.setEntity(payload);
HttpResponse result = client.execute(request);
The response shows HTTP error code: 400. Can anyone help me understand what’s wrong with my request format or required fields?
Hit this same issue two weeks ago with our JIRA setup. Besides the JSON formatting stuff others mentioned, check your project parameter format. Don’t just pass the project key as a string - you might need it as an object depending on your JIRA version. Change “project”:“DEMO” to “project”:{“key”:“DEMO”}. Also, that leadUserName field might be causing problems if the user doesn’t have permissions or doesn’t exist in the project. Try removing leadUserName completely and see if the component creates with defaults.
Your JSON payload is missing curly braces! It should be wrapped like {“name”:“TestComponent”,…}. Also, try removing that trailing slash from the endpoint URL. Had the same issue last month and that’s what fixed it for me.
Yeah, it’s your JSON structure, but there’s another issue too. The isAssigneeTypeValid field needs to be a boolean, not a string. Change "isAssigneeTypeValid":"false" to "isAssigneeTypeValid":false (no quotes around false). Also double-check that your project key actually exists. I’ve hit similar auth problems before - some JIRA instances get weird about the Content-Type header. Try adding request.setHeader("Content-Type", "application/json"); before you execute the request. Here’s what your corrected JSON should look like: {"name":"TestComponent","description":"Sample component for testing","leadUserName":"admin","assigneeType":"PROJECT_LEAD","isAssigneeTypeValid":false,"project":"DEMO"}