1

我使用 Jenkins 作为持续集成工具。我正在使用 Jira 插件,以便它可以更新 Jira 中的问题。但是那里缺少一些东西,你不能用它来更新版本的描述。无论如何,我决定使用 Jira REST api。我在一行中找到了一种使用 curl 的非常简单的方法:

curl -i -u user:password -H "Content-Type: application/json" -X PUT --data '{"name":"V55"}' http://jira/rest/api/latest/version/10107

如您所见,在调用它之前我需要获取 2 个参数(数据和 url),这就是事情变得复杂的地方。我可以很容易地用 Jenkins 获取数据,问题在于 url。版本(在示例中为 10107)是您需要使用 GET 请求检索的字符串,然后解析结果(它是 JSON)。再一次,在 curl 中真的很容易:

curl -i -u user:password -H "Content-Type: application/json" -X GET http://jira/rest/api/latest/project/SBX

所以基本上,我需要的是解析结果以获取版本,然后调用我的 PUT。我无法做到这一点。我在 ANT 中尝试过,我设法解析 JSON,但由于引号,它不会让我调用我的 curl 行。我尝试在 ANT 中使用 groovy 脚本,但无法加载 groovy 库。(我已经花了 4 天时间)

现在我想用 C# 来做(知道我不熟悉 web 服务),但是每次我找到一个如何做的例子时,都会出错。有时它没有身份验证,有时 Visual Studio 正在开玩笑。

我遵循了这个非常清楚的教程:http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-Consuming-REST-services-with-HttpClient/但是当我用 httpclient.put 替换 httpclient.post 时,它向我展示了它对参数的期望,以及我何时编写 ; 为了完成这一行,VS2012 用红色下划线表示“Microsoft.http.HttpClient”不包含“Put”的定义。

TL;博士:

我正在寻找一种简单的方法来使用 JSON 格式的数据在 REST 服务上实现 PUT 请求。无论是在 ANT 中还是在 C# 中。谢谢。

编辑:这就是我的代码现在的样子:

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using RestSharp;


            namespace jira_rest_api_test
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        RestClient Client = new RestClient
                        {
                            BaseUrl = "http://jira/rest/api/latest/",
                            Authenticator = new HttpBasicAuthenticator("user", "password")
                        };
                        RestRequest request = new RestRequest("version/10107", Method.PUT);
                        request.AddHeader("Accept", "application/json");
                        request.AddHeader("Content-Type", "application/json; charset=utf-8");
                        request.RequestFormat = DataFormat.Json;
                        request.AddBody("{\"name\":\"v23\"}");
                        var response = Client.Execute(request);
                        Console.WriteLine(request.JsonSerializer.ContentType);
                        Console.WriteLine(request.Resource);
                        Console.WriteLine(request.Parameters.Count);
                        Console.WriteLine(response.Content);

                    }
                }
            }

这是我得到的错误:

{"errorMessages":["Can not instantiate value of type [simple type, class com.atlassian.jira.rest.v2.issue.version.VersionBean] from JSON String; no single-String constructor/factory method"]}
4

0 回答 0