0

API を使用して JIRA で課題を作成しようとしています。クロムで郵便配達員を使用している場合、正常に動作しています。しかし、.net コードを使用して実行しようとすると、エラーが発生します。

返されるエラーは「400 Bad Request」です。

これに関連して StackOverflow と Web で広範な検索を行いましたが、解決策が見つからないか、提供された解決策が役に立ちません。

これは私のコードです:

        string data = @"{ ""fields"":{""project"":{""key"":""TES""},""summary"":""sum1"",""issuetype"":{""name"":""Bug""},""duedate"":""2013-01-24 09:00 AM +05:30"",""customfield_10000"":[{""value"":""None - Nothing""}],""reporter"":{""name"":""praveen_tadikemalla""}}}";


       //tried  by using new class Issue also
       /* Issue data = new Issue();
        data.fields = new Fields();
        data.fields.description = "test";
        data.fields.summary = "test bugs";

        data.fields.issuetype = new IssueType();
        data.fields.issuetype.name = "bug";

        data.fields.project = new Project();
        data.fields.project.key = "TES";         
       */

        string postUrl = "http://localhost:8080/rest/auth/latest/session";

        System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
        client.BaseAddress = new System.Uri(postUrl);


        byte[] cred = UTF8Encoding.UTF8.GetBytes("amit:GoMessiGo");

        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
        client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

        System.Net.Http.Formatting.MediaTypeFormatter jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();  
        System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<string>(data,jsonFormatter);

       //in case if i use custom Issue class   
       // System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Issue>(data,jsonFormatter);


        System.Net.Http.HttpResponseMessage response = client.PostAsync("http://localhost:8080/rest/api/latest/issue/", content).Result;     

        string result=null;

        if (response.IsSuccessStatusCode)          
        {
            result = response.Content.ReadAsStringAsync().Result;
        }
        else
        {
           result  = response.StatusCode.ToString();
        }
      return result;

ヘッダーで承認を送信しました。

get リクエストの API をヒットできます。

私が疑うのは、このメソッドを使用してサーバーに投稿リクエストを送信する際に問題があることです.(client.PostAsync)

   System.Net.Http.HttpResponseMessage response = client.PostAsync("http://localhost:8080/rest/api/latest/issue/", content).Result;    

または

このコードスニペット

    System.Net.Http.HttpContent content = new System.Net.Http.ObjectContent<Sting>(data,jsonFormatter); 

しかし、これらは私の仮定です

4

1 に答える 1

0

私はそれを働かせました。

私はこのリンクをたどりました。

Rest c# httpClient を使用して Jira 課題を作成する

プロジェクトでこの System.Net.Http.Formatting を使用していました。このリンクは、Webアプリケーションでのみ機能すると述べています。

私の場合、コンソールアプリケーションで他のタイプのプロジェクトを作成している場合。別の Json 逆シリアル化クラスを使用する必要があります。または StringContent クラスを使用できます。

そのため、String Content クラスを使用して問題を修正しました

于 2014-07-15T13:09:12.393 に答える