不允许返回Jira API POST(405)方法

时间:2018-07-06 23:43:58

标签: c# rest jira

我似乎无法弄清为什么我收到405“方法不允许”错误消息。

我想在我的项目中创建一个新期刊,密钥为“ BS”。我直接从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();

2 个答案:

答案 0 :(得分:0)

https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-post

1-尝试使用HTTPS

2-您有WRITE范围吗?

3-响应返回什么内容? Could be something

4-“您必须在父字段中提供父问题的ID或密钥。”

答案 1 :(得分:0)

所以对于任何有类似问题的人,我想留下这个答案来说明如何解决。

首先,我废弃了整个项目,而是使用RestSharp进行连接。我用邮递员来测试我的json,直到正确为止。邮递员可以选择将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();
        }
    }