I want to create issue in existing project in jira using java program. please suggest me how to do.
Hi Vineet,
You should have a look at the following Developer Resource: Java APIs. Or have a look at an older question which could give you something to start: Create issue via REST API useing Java
Hope this helps.
Best,
Maarten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vineet,
401 means something is not right with the authentication. Have a look at the following line of code and make sure to adjust it to your credentials:
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vzverev:Gromozavr".getBytes(), 0));
Best,
Maarten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
inputstream variable is not used
package NewPost;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.*;
import java.net.*;
import javax.json.*;
public class testREST_CreateIssue {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);
HttpURLConnection conn = (HttpURLConnection) jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");
System.out.println(getJSON_Body() + "/" + encodedData);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());
try {
InputStream inputStream = conn.getInputStream();
}
catch (IOException e){
System.out.println(e.getMessage());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10105"))
).build();
return createIssue.toString();
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
if i remove inputstream then it gives error at
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("vineet1492:742762".getBytes(), 0));
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still getting error 400 in the code.
import java.io.*;
import java.net.*;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import javax.json.*;
public class testREST_POST_CreateIssues {
public static void main(String[] args) {
try {
URL jiraREST_URL = new URL("http://localhost:8080/rest/api/2/issue/");
/* Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user,pass.toCharArray());
}
});
*/
URLConnection urlConnection = jiraREST_URL.openConnection();
urlConnection.setDoInput(true);
HttpURLConnection conn = (HttpURLConnection)jiraREST_URL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
String encodedData = URLEncoder.encode(getJSON_Body(),"UTF-8");
System.out.println(getJSON_Body() + "/" + encodedData);
System.out.println(getJSON_Body());
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "Basic " + Base64.encode("username:password".getBytes(),0));
conn.setRequestProperty("Content-Type", "application/json");
System.out.println(conn);
conn.setRequestProperty("Content-Length", String.valueOf(encodedData.length()));
conn.getOutputStream().write(encodedData.getBytes());
try {
InputStream inputStream = conn.getInputStream();
System.out.println(inputStream);
}
catch (IOException e){
System.out.println(e.getMessage());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static String getJSON_Body(){
JsonObject createIssue = Json.createObjectBuilder()
.add("fields",
Json.createObjectBuilder().add("project",
Json.createObjectBuilder().add("key","USC"))
.add("summary", "sum")
.add("description", "descr")
.add("issuetype",
Json.createObjectBuilder().add("id", "10015"))
).build();
return createIssue.toString();
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.