ZenDesk使用RestSharp时出错:名称太短

时间:2014-05-14 20:24:11

标签: c# json restsharp zendesk

当我尝试运行下面的代码时,我有错误代码:名称太短(最少1个字符)。我正在使用RestSharp v104.1。如果我尝试阅读,就在我尝试创建新用户时,我没有任何问题。

static string requestUri = "https://subdomain.zendesk.com/api/v2";

    static string username = "myusername";
    static string password = "mypassword";

    static void Main(string[] args)
    {
        string new_users = "{\"user\":{ \"id\":4000000001, \"name\":\"Robin 001\", \"email\":\"robin.001@company.com\" }}";

        Console.WriteLine(WriteJSON(username, password, "/users.json", new_users));
        Console.ReadLine();
    }

    public static string WriteJSON(string username, string password, string json, string to_write)
    {
        var client = new RestClient(requestUri);
        client.Authenticator = new HttpBasicAuthenticator(username, password);
        client.AddDefaultHeader("Content-type", "application/json");
        client.UserAgent = "MozillaXYZ/1.0";
        client.FollowRedirects = true;
        client.MaxRedirects = 10;

        var request = new RestRequest(json);
        request.Method = Method.POST;
        request.RequestFormat = DataFormat.Json;
        request.AddBody(to_write);

        IRestResponse response = client.Execute(request);
        var content = response.Content;

        System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(content, "root");
        return node.ToString();
    }

1 个答案:

答案 0 :(得分:0)

我在另一个地方发布了同样的问题,这是帮助我的答案: https://github.com/restsharp/RestSharp/issues/535#issuecomment-43373031