Jira API POST 返回 (405) 方法不允许
Jira API POST returning (405) Method Not Allowed
我似乎无法弄清楚为什么我会收到 405 "Method Not Allowed" 错误消息..
我想在我的项目中创建一个新问题,密钥是"BS"。我 copied/pasted 这直接来自 Jira 文档,但仍然无法正常工作。
我尝试了很多不同类型的字符串变体,但这个应该有用。是否有其他原因可能会给我这个错误?
这是我的代码:
string stringData = @"{""fields"": {""project"":{""key"": ""BS""},""summary"": ""REST ye merry gentlemen."",""issuetype"": {""name"": ""Ticket""}}}";
string url = @"http://HOST.atlassian.net/rest/api/2/issue";
var data = Encoding.ASCII.GetBytes(stringData); // or UTF8
WebRequest wrUrl = WebRequest.Create(url);
wrUrl.ContentType = "application/json";
wrUrl.Method = "POST";
wrUrl.Headers["Authorization"] = "Basic " + Convert
.ToBase64String(Encoding.ASCII.GetBytes(Username+":"+Password));
wrUrl.ContentLength = data.Length;
var newStream = wrUrl.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
Console.WriteLine(wrUrl.GetResponse().ToString());
Console.ReadKey();
https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-post
1 - 尝试 HTTPS
2 - 你有 WRITE 范围吗?
3 - 响应会返回哪些内容? Could be something
4 - "You must provide a parent field with the ID or key of the parent issue."
所以对于遇到类似问题的任何人,我想留下这个答案以展示我是如何修复它的。
首先,我放弃了整个项目,转而使用 RestSharp 进行连接。我使用 Postman 测试我的 json 直到我做对了。 Postman 可以选择将您的 JSON 转换为 C# 请求,这是我的代码:
using System;
using RestSharp;
using RestSharp.Authenticators;
public class Program
{
//This gets meta tags for the SS project and issue type "Ticket"
// https://HOST.atlassian.net/rest/api/2/issue/createmeta?projectKeys=SS&issuetypeNames=Ticket&expand=projects.issuetypes.fields
public static string url = "https://HOST.atlassian.net/rest/api/2/issue";
public static string username = "YOURUSERNAME";
public static string password = "YOURAPIKEY";
//host:port/context/rest/api-name/api-version/resource-name
static void Main(string[] args)
{
var fieldPriority = "Low";
var fieldSubject = "Payment Problem";
var fieldTechResource = "BLAH";
NewJiraIssue(fieldSubject,fieldPriority, fieldTechResource);
}
//*****************************************
// Create a new Jira Issue
//*****************************************
//INPUT: OUT: New Ticket Number
//*****************************************
static void NewJiraIssue(string fieldSubject, string fieldPriority, string fieldTechResource)
{
try
{
var client = new RestClient("https://HOST.atlassian.net/rest/api/latest/issue");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "ad6dbffb-89fe-4b21-b59a-0dc60724510f");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Basic
KEY FROM ");
request.AddHeader("Content-Type", "application/json");
//Created Date
//Date CR Is approved internal //Customer Approval
Sent Date //Customer Approval Date:
//Planned UAT Completion Date: //Actual Delivery
Date to UAT:
request.AddParameter("undefined", "{\r\n \"fields\": {\r\n
\"project\":\r\n {\r\n \"key\": \"SS\",\r\n \"id\":
\"10040\"\r\n },\r\n \"summary\": \""+fieldSubject+"\",\r\n \r\n
\"issuetype\": {\r\n \"name\": \"Ticket\"\r\n },\r\n
\"customfield_10042\" : \"2010-03-01T00:00:00.000+0400\",\r\n
\"customfield_10043\" : \"customfield_10043\",\r\n\r\n \"customfield_10067\" :
\"customfield_10067\",\r\n \"customfield_10068\" :
\"customfield_10068\",\r\n\r\n \"customfield_10070\" : \"2001-03-
01T00:00:00.000+0400\",\r\n \"customfield_10071\" : \"2002-03-
01T00:00:00.000+0400\",\r\n \"customfield_10072\" : \"2003-03-
01T00:00:00.000+0400\",\r\n \"customfield_10073\" : \"2004-03-
01T00:00:00.000+0400\",\r\n \"customfield_10074\" : \"2005-03-
01T00:00:00.000+0400\",\r\n \r\n \"priority\": {\"name\": \"" +
fieldPriority + "\"},\r\n \"customfield_10069\": {\"name\" : \"" +
fieldTechResource + "\"}\r\n}\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
}
刚刚遇到这个确切的错误:状态代码 405“方法不允许”。事实证明,答案实际上只是使用 HTTPS 而不是 HTTP。接受的答案缺少此详细信息。
我似乎无法弄清楚为什么我会收到 405 "Method Not Allowed" 错误消息..
我想在我的项目中创建一个新问题,密钥是"BS"。我 copied/pasted 这直接来自 Jira 文档,但仍然无法正常工作。
我尝试了很多不同类型的字符串变体,但这个应该有用。是否有其他原因可能会给我这个错误?
这是我的代码:
string stringData = @"{""fields"": {""project"":{""key"": ""BS""},""summary"": ""REST ye merry gentlemen."",""issuetype"": {""name"": ""Ticket""}}}";
string url = @"http://HOST.atlassian.net/rest/api/2/issue";
var data = Encoding.ASCII.GetBytes(stringData); // or UTF8
WebRequest wrUrl = WebRequest.Create(url);
wrUrl.ContentType = "application/json";
wrUrl.Method = "POST";
wrUrl.Headers["Authorization"] = "Basic " + Convert
.ToBase64String(Encoding.ASCII.GetBytes(Username+":"+Password));
wrUrl.ContentLength = data.Length;
var newStream = wrUrl.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
Console.WriteLine(wrUrl.GetResponse().ToString());
Console.ReadKey();
https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-post
1 - 尝试 HTTPS
2 - 你有 WRITE 范围吗?
3 - 响应会返回哪些内容? Could be something
4 - "You must provide a parent field with the ID or key of the parent issue."
所以对于遇到类似问题的任何人,我想留下这个答案以展示我是如何修复它的。
首先,我放弃了整个项目,转而使用 RestSharp 进行连接。我使用 Postman 测试我的 json 直到我做对了。 Postman 可以选择将您的 JSON 转换为 C# 请求,这是我的代码:
using System;
using RestSharp;
using RestSharp.Authenticators;
public class Program
{
//This gets meta tags for the SS project and issue type "Ticket"
// https://HOST.atlassian.net/rest/api/2/issue/createmeta?projectKeys=SS&issuetypeNames=Ticket&expand=projects.issuetypes.fields
public static string url = "https://HOST.atlassian.net/rest/api/2/issue";
public static string username = "YOURUSERNAME";
public static string password = "YOURAPIKEY";
//host:port/context/rest/api-name/api-version/resource-name
static void Main(string[] args)
{
var fieldPriority = "Low";
var fieldSubject = "Payment Problem";
var fieldTechResource = "BLAH";
NewJiraIssue(fieldSubject,fieldPriority, fieldTechResource);
}
//*****************************************
// Create a new Jira Issue
//*****************************************
//INPUT: OUT: New Ticket Number
//*****************************************
static void NewJiraIssue(string fieldSubject, string fieldPriority, string fieldTechResource)
{
try
{
var client = new RestClient("https://HOST.atlassian.net/rest/api/latest/issue");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "ad6dbffb-89fe-4b21-b59a-0dc60724510f");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Basic
KEY FROM ");
request.AddHeader("Content-Type", "application/json");
//Created Date
//Date CR Is approved internal //Customer Approval
Sent Date //Customer Approval Date:
//Planned UAT Completion Date: //Actual Delivery
Date to UAT:
request.AddParameter("undefined", "{\r\n \"fields\": {\r\n
\"project\":\r\n {\r\n \"key\": \"SS\",\r\n \"id\":
\"10040\"\r\n },\r\n \"summary\": \""+fieldSubject+"\",\r\n \r\n
\"issuetype\": {\r\n \"name\": \"Ticket\"\r\n },\r\n
\"customfield_10042\" : \"2010-03-01T00:00:00.000+0400\",\r\n
\"customfield_10043\" : \"customfield_10043\",\r\n\r\n \"customfield_10067\" :
\"customfield_10067\",\r\n \"customfield_10068\" :
\"customfield_10068\",\r\n\r\n \"customfield_10070\" : \"2001-03-
01T00:00:00.000+0400\",\r\n \"customfield_10071\" : \"2002-03-
01T00:00:00.000+0400\",\r\n \"customfield_10072\" : \"2003-03-
01T00:00:00.000+0400\",\r\n \"customfield_10073\" : \"2004-03-
01T00:00:00.000+0400\",\r\n \"customfield_10074\" : \"2005-03-
01T00:00:00.000+0400\",\r\n \r\n \"priority\": {\"name\": \"" +
fieldPriority + "\"},\r\n \"customfield_10069\": {\"name\" : \"" +
fieldTechResource + "\"}\r\n}\r\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.ReadKey();
}
}
刚刚遇到这个确切的错误:状态代码 405“方法不允许”。事实证明,答案实际上只是使用 HTTPS 而不是 HTTP。接受的答案缺少此详细信息。