简体   繁体   中英

Jira REST API returns error when creating issue

My goal is to use the Jira REST API to create an issue. But as of now I am getting an error (405) Method Not Allowed.

I have checked in the Jira Properties if the Jira Remote APIs are switched on. Under:

JIRA Configuration > General Configuration > Allow Remote API Calls is ON.

I have also checked my JSON string submitted which looks fine to me:

"{\"fields\":{\"project\":{\"key\":\"CTTS\"},\"summary\":\"Api  Test\",\"description\":\"Test\",\"issuetype\":{\"name\":\"Story\"}}}"

My current code to call the API is following:

public JiraApiResponseObject CreateRequest(JSONstring)
    {
        jiraRequest = (HttpWebRequest)WebRequest.Create("https://MyJiraUrl.net/rest/api/2/issue");

        jiraRequest.Method = "POST";
        jiraRequest.ContentType = "application/json";
        jiraRequest.Accept = "application/json";

        using (var streamWriter = new StreamWriter(jiraRequest.GetRequestStream()))
        {
            streamWriter.Write(JSONstring);
            streamWriter.Flush();
        }

        HttpWebResponse response = (HttpWebResponse)jiraRequest.GetResponse();
        return jiraReturnObject;
    }

I currently dont know what is causing the error. I just get the (405) Method Not Allowed error from the remote server, when i actually would expect a API response.

There Is an Authorization Header Missing in the WebRequest. Jira needs such a header to confirm that only authorized users can access the API's.

This Authorization String is is build as follows:

"Basic username:api_token"

The api_token can be generated in the Jira Cloud and needs to be base64-encoded.

Here is what I would do:

  1. Generate an API token for Jira using your Atlassian Account: https://id.atlassian.com/manage/api-tokens .
  2. Build a string of the form username:api_token.
  3. Base64-encode encode the string.
  4. Supply an Authorization header with content Basic followed by the encoded string.

For example, the string fred:fred encodes to ZnJlZDpmcmVk in base64, so you would add the following to your request:

jiraRequest.Headers["Authorization"] = "Basic " + "UserName" + Base64Encode(apiToken);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM