Hello
I am preparing some java pojo to upload some users and projects to our JIRA instance.
I tried to perform the login via REST API and it seems to work (I have back the JSESSIONID). Then when I try to perform a GET (e.g. read an issue) it works but when I try a POST (e.g. create a user) it fails with HTTP error 401.
Following the code for doing the login
URL url = new URL(jiraUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("username", user);
json.put("password", pwd);
OutputStream os = conn.getOutputStream();
os.write(json.toString().getBytes());
os.flush();
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
while ((output = br.readLine()) != null) {
JSONObject response = new JSONObject(output);
JSONObject session = new JSONObject(response.get("session").toString());
cookie = session.get("name").toString() + "=" + session.get("value").toString();
}
conn.disconnect();
And I get back the session cookie
{"session":{"name":"JSESSIONID","value":"CF338E42E427A53D556811037D83268E"},"loginInfo":{"failedLoginCount":18,"loginCount":533,"lastFailedLoginTime":"2018-01-08T15:10:00.340+0000","previousLoginTime":"2018-07-24T07:58:12.972+0000"}}
Then I want to create a new user, and I used the cookie variable as
JSESSIONID=CF338E42E427A53D556811037D83268E
URL url = new URL(jiraUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "application/json");
//session cookie
conn.setRequestProperty("Authorization", "Cookie");
conn.setRequestProperty("cookie", cookie);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
JSONObject json = new JSONObject();
json.put("name", _user);
json.put("password", _pwd);
json.put("emailAddress", _email);
json.put("displayName", _fullname);
String[] applicationKeys = { "jira-core" };
json.put("applicationKeys", applicationKeys);
OutputStream os = conn.getOutputStream();
os.write(json.toString().getBytes());
os.flush();
if (conn.getResponseCode() == HttpURLConnection.HTTP_CREATED) {
created = true;
Log.log("Created user " + _user + "...");
} else if (conn.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST)
Log.log(">> 400: request is invalid <<");
else if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED)
Log.log(">> 401: user is not authenticated <<");
else if (conn.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN)
Log.log(">> 403: caller user does not have permission to create the user <<");
else if (conn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR)
Log.log(">> 500: user was not created because of other error <<");
conn.disconnect();
Here I get HTTP 401…So how should I modify my java code?
Thanks in advance!