【问题标题】:Cannot PUT/POST using Rest Sharp using XML无法使用 XML 使用 Rest Sharp 进行 PUT/POST
【发布时间】:2012-11-16 02:14:38
【问题描述】:

我想使用 restSharp 的 PUT/POST 方法在 redmine 上创建/修改问题。 我无法使用 Rest sharp 找到有关 xml PUT/POST 的有价值信息。我尝试了来自 restsharp.org 的各种方法,例如 Addbody("test", "subject");IRestResponse response = client.Execute(request);,但 Redmine 没有任何变化。我做错了什么?

POST 给出“只允许获取、放置和删除请求”。消息。

PUT 给出“仅允许获取、发布和删除请求”。消息。

我的代码

    RestClient client = new RestClient(_baseUrl);
    client.Authenticator = new HttpBasicAuthenticator(_user, _password);


    RestRequest request = new RestRequest("issues/{id}.xml", Method.POST);

    request.AddParameter("subject", "Testint POST");

    request.AddUrlSegment("id", "5");


    var response = client.Execute(request);

【问题讨论】:

  • 至少解释一下你为什么不赞成这篇文章。
  • Conrad 看看这个 StackOverFlow 链接 stackoverflow.com/questions/10747261/… 如果这不能帮助在 C# Put/GET RestSharp 示例上进行谷歌搜索
  • 我做到了,并且给出的唯一示例是 JSON 而不是 xml,我也尝试将他们的示例调整为 xml,它仍然没有改变 redmine。
  • 如果您想使用 XML 执行此操作,您可能需要更改您的问题以反映这一点..
  • 这里是另一个链接..如果你有 XML 也请显示 XML 的结构..stackoverflow.com/questions/8917437/…

标签: c# xml http rest restsharp


【解决方案1】:

问题出在序列化中。我的Issue 类包含导致序列化问题的各种其他类的对象。 我们就是这样做的:

    RestRequest request = new RestRequest("issues/{id}.xml", Method.PUT);
    request.AddParameter("id", ticket.id, ParameterType.UrlSegment);
    request.XmlSerializer = new RedmineXmlSerializer();
    request.AddBody(ticket);

    RestClient client = new RestClient(_baseUrl);
    client.Authenticator = new HttpBasicAuthenticator(_user, _password);
    IRestResponse response = client.Execute(request);

【讨论】:

    【解决方案2】:

    您的代码对我来说看起来不错,我不确定您是否需要这个,但我们在针对 WebAPI 主机使用 RestSharp for json 时添加了此标头:

            request.AddHeader("Accept", "application/xml");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2011-11-27
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      相关资源
      最近更新 更多