[英]RestSharp POST request translation from cURL request
我正在嘗試使用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
注意:我也嘗試了這篇文章中的建議,但是沒有解決問題。 任何指導表示贊賞!
不要
request.AddParameter("data", request.JsonSerializer.Serialize(issueToCreate));
而是嘗試:
request.AddBody(issueToCreate);
您可以使用的清潔且更可靠的解決方案如下所述:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.