将JIRA API cURL转换为c#http请求

时间:2019-12-20 14:23:15

标签: c# .net rest api jira

我有一个cURL,通过它可以连接到Atlassian JIRA API,并搜索有关JQL的问题,并按项目名称和状态过滤问题。

这是cURL命令,效果很好:

curl -u JIRAUSER:JIRATOKEN -X POST --data '{ "jql": "project = \"QA\" AND status=\"To Do\" " }' -H "Content-Type: application/json" https://jiraserver.atlassian.net/rest/api/2/search

我正在尝试使用httpclient POST方法在c#上重建它。代码如下:

static async System.Threading.Tasks.Task Main(string[] args)
        {
            using (var httpClient = new HttpClient())
            {
                using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://jiraserver.atlassian.net/rest/api/2/search"))
                {
                    var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("JIRAUSER:JIRA TOKEN"));
                    request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

                    request.Content = new StringContent("{ \"jql\": \"project = \"QA\" AND status = \"To Do\" }");
                    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                    var response = await httpClient.SendAsync(request);

                    Console.WriteLine(response);


                }
            }

        }

响应返回以下错误:

  

StatusCode:400,ReasonPhrase:“”,版本:1.1,内容:   System.Net.Http.HttpConnectionResponseContent

在System.Diagnostics.Debug.Write中,出现以下错误:

  

错误CS0428:无法将方法组“写入”转换为非委托类型   '宾语'。您打算调用该方法吗?

请给我一些提示,否则我将要吊死自己...

1 个答案:

答案 0 :(得分:0)

我建议使用JIRA SDK与您的JIRA实例-> https://bitbucket.org/farmas/atlassian.net-sdk/src/master/进行交互。

基本上,步骤集是

1) initiate jira client:

    var jiraClient = Jira.CreateRestClient(<jiraURL>, <jiraUserName>, <jiraUserPwd>);

2) connect to jira, and pull based on search criteria:

     var jql =  "project = QA + " AND status in (To Do)";
    IEnumerable<Atlassian.Jira.Issue> jiraIssues = AsyncTasker.GetIssuesFromJQL(jiraClient, jql, 999);

3) then, you can enumerate in the pulled issues

    ...
    foreach(var issue in jiraIssues)
    {   
        /*      
        ... for example, some of the available attributes are:

                    issue.Key.Value
                    issue.Summary
                    issue.Description
                    issue.Updated
                    String.Format("{0}/browse/{1}", jiraURL.TrimEnd('/') , issue.Key.Value)
        ...
        */
    }                    
  • 在旁注中,建议不要使用using(... = new HttpClient()){....}