[英]RestSharp POST request translation from cURL request
I'm trying to make a POST request using RestSharp to create an issue in JIRA, and what I have to work with is an example that uses cURL. 我正在尝试使用RestSharp发出POST请求以在JIRA中创建问题,而我必须使用的是使用cURL的示例。 I'm not familiar with either enough to know what I'm doing wrong.
我不熟悉或者不知道我做错了什么。
Here's the example given in cURL: 这是cURL中给出的示例 :
curl -D- -u fred:fred -X POST --data {see below} -H "Content-Type: application/json"
http://localhost:8090/rest/api/2/issue/
Here's their example data: 这是他们的示例数据:
{"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"}}}
And here's what I'm attempting with RestSharp: 这就是我正在尝试使用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);
What I get back is a 415 response of 我得到的是415的回复
Unsupported Media Type
Note: I also tried what was suggested in this post , but that did not resolve the issue. 注意:我也尝试了这篇文章中的建议,但是没有解决问题。 Any guidance is appreciated!
任何指导表示赞赏!
dont do 不要
request.AddParameter("data", request.JsonSerializer.Serialize(issueToCreate));
instead try: 而是尝试:
request.AddBody(issueToCreate);
A clean and more reliable solution you could use is described below: 您可以使用的清洁且更可靠的解决方案如下所述:
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);
The full code I uploaded to gist: https://gist.github.com/gandarez/50040e2f94813d81a15a4baefba6ad4d 我上传到gist的完整代码: 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 Jira文档: https : //developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-create-issue
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.