Reputation: 159
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.
Upvotes: 0
Views: 1667
Reputation: 159
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:
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);
Upvotes: 2