java - 如何使用 REST API 和 POST 问题登录 JIRA

标签 java rest

实际上,我的任务是从 Excel 工作表中发布 JIRA 中的错误。

现在我可以在 Bugzilla 中发布错误。请任何人帮助我编写有关如何使用 REST API 登录和发布错误的 Java 代码。我有一个 Perl 语言的示例,我想要 Java 语言的示例。

我尝试使用以下代码,我可以登录,但现在我想使用 JIRA REST API 执行所有任务。

HttpPost httpost = new HttpPost("https://id.atlassian.com/login?continue=https://jira.atlassian.com/secure/Dashboard.jspa&application=jac");

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("username", "sxxxxxxx@live.com"));
    nvps.add(new BasicNameValuePair("password", "xxxxxxxxxn"));
    nvps.add(new BasicNameValuePair("csrfToken", xcrfTokenVal));
    nvps.add(new BasicNameValuePair("continue", "https://jira.atlassian.com/secure/Dashboard.jsp"));
    nvps.add(new BasicNameValuePair("application", "jac"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps));

       response = httpclient.execute(httpost);

    System.out.println("Response " + response.toString());
     entity = response.getEntity();
            //System.out.println("Double check we've got right page " + EntityUtils.toString(entity));
    System.out.println("Response from : " + response.getStatusLine());

最佳答案

您必须对“http://JIRA_URL/rest/auth/1/session ”执行 POST,并使用 JSessionID 设置 Cookie 才能执行其他请求。在 Jersey ,基本上你会这样做:

jsonParameters = "{\"username\":\"usernameValue\", \"password\": \"passwordValue\"}";
WebResource webResource = client.resource("http://<JIRA_URL>/rest/auth/1/session");
ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).post(ClientResponse.class, jsonParameters);

在响应中,您将获得 JSessionID,因此您必须将其保存在 Cookie 中并使用它。在 Jersey :

import javax.ws.rs.core.Cookie;
...
Cookie cookie = new Cookie(jsession.getName(), jsession.getValue());
response = webResource.type(MediaType.APPLICATION_JSON).cookie(cookie)
                .post(ClientResponse.class, jsonParameters);

JIRA Rest 文档:

https://docs.atlassian.com/jira/REST/6.3.1/#d2e3131

关于java - 如何使用 REST API 和 POST 问题登录 JIRA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30183702/

相关文章:

java - 枚举中的标准 SQLState

Java赋值,为不同类中的变量添加值

java - 用于更新具有空值或 null 值的信息的 REST API

rest - tshark 数据包捕获过滤器按请求 url

java - 是否可以知道注释方法是否被覆盖( boolean 值)?

java - 注释@ConditionalOnMissingBean 在测试中不匹配

java - 在调用正确的@Path 后,Jersey 返回 404

rest - 如何通过 Azure DevOps API 在给定的看板栏中找到所有工作项?

java - 如何动态指定Spring Restful WebService URL?

javascript - Backbone 破坏没有服务器通信