RestSharp POST request translation from cURL request

后端 未结 2 904
栀梦
栀梦 2021-01-21 00:37

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. I\'m not familiar with either enough to

2条回答
  •  北海茫月
    2021-01-21 01:24

    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(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

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

提交回复
热议问题