2012-11-14 362 views
3

我想使用RestSharp在JIRA中创建一个POST请求来创建问题,我必须使用的是使用cURL的示例。我不知道我做错了什么。从cURL请求翻译RestSharp POST请求

下面是卷曲给出的example

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 

注意:我也尝试了this post中提出的建议,但是这并没有解决问题。任何指导表示赞赏!

回答

3

不做

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

,而不是尝试:

request.AddBody(issueToCreate); 
+0

谢谢沃尔!对于任何发现这篇文章的人来说,因为它特别与JIRA有关,我还发现Atlassian的REST服务(也许这是全面的)是区分大小写的!在我的JiraIssue类中,我创建了诸如“Key”和“Summary”之类的属性,但它必须是“key”和“summary”...... doh! – gopherr

+1

对于任何感兴趣的人,我已经在JIRA REST客户端和WPF应用程序[此处](https://bitbucket.org/grodgers/jiravu/overview)中实现了此RestSharp POST。 – gopherr

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); 

完整的代码我上传到要点:https://gist.github.com/gandarez/50040e2f94813d81a15a4baefba6ad4d

Jira documentation: https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-create-issue