3

RestSharp を使用して POST リクエストを作成し、JIRA で問題を作成しようとしています。作業する必要があるのは、cURL を使用する例です。私は自分が間違っていることを知るのに十分なほど、どちらにも精通していません。

cURLでの例を次に示します。

curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json"
http://localhost:8090/rest/api/2/issue/

サンプルデータは次のとおりです。

{"fields":{"project":{"key":"TEST"},"summary":"REST ye merry gentlemen.","description":"Creating of an issue using project keys and issue type names using the REST API","issuetype":{"name":"Bug"}}}

そして、これが私がRestSharpで試みていることです:

RestClient client = new RestClient();
client.BaseUrl = "https://....";
client.Authenticator = new HttpBasicAuthenticator(username, password);
....// connection is good, I use it to get issues from JIRA
RestRequest request = new RestRequest("issue", Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddParameter("data", request.JsonSerializer.Serialize(issueToCreate));
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);

私が返すのは、の415応答です

Unsupported Media Type

注:この投稿で提案されていることも試しましたが、問題は解決しませんでした。任意のガイダンスをいただければ幸いです。

4

2 に答える 2

3

しないでください

request.AddParameter("data", request.JsonSerializer.Serialize(issueToCreate));

代わりに試してください:

request.AddBody(issueToCreate);
于 2012-11-15T23:47:15.417 に答える
3

使用できるクリーンで信頼性の高いソリューションを以下に示します。

var client = new RestClient("http://{URL}/rest/api/2");
var request = new RestRequest("issue/", Method.POST);

client.Authenticator = new HttpBasicAuthenticator("user", "pass");

var issue = new Issue
{
    fields =
        new Fields
        {
            description = "Issue Description",
            summary = "Issue Summary",
            project = new Project { key = "KEY" }, 
            issuetype = new IssueType { name = "ISSUE_TYPE_NAME" }
        }
};

request.AddJsonBody(issue);

var res = client.Execute<Issue>(request);

if (res.StatusCode == HttpStatusCode.Created)
    Console.WriteLine("Issue: {0} successfully created", res.Data.key);
else
    Console.WriteLine(res.Content);

Gist にアップロードした完全なコード: https://gist.github.com/gandarez/50040e2f94813d81a15a4baefba6ad4d

Jira ドキュメント: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-create-issue

于 2016-08-25T13:30:08.043 に答える