RestSharp PUT XML,RestSharp将其作为GET发送?

时间:2014-08-07 23:33:51

标签: c# xml restsharp

我正在尝试使用prestashop API编辑产品,使用XML在C#中使用RestSharp。文档的说明如下:

To edit an existing resource: GET the full XML file for the resource you want to change (/api/customers/7), edit its content as needed, then PUT the whole XML file back to the same URL again.

我正在尝试编辑/ customers / 1.

我的GET调用正常工作以检索数据。我现在正在反序列化数据,根据需要进行编辑,然后重新保存到XML文件。一切似乎进展顺利。我现在尝试改变的唯一字段是名字和姓氏。其余数据未受影响。这是我正在使用的XML的副本:

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
  <customer>
    <id><![CDATA[1]]></id>
    <id_default_group xlink:href="http://heatherfazelinia.com/api/groups/3"><![CDATA[3]]></id_default_group>
    <id_lang xlink:href="http://heatherfazelinia.com/api/languages/1"><![CDATA[1]]></id_lang>
    <newsletter_date_add><![CDATA[2013-12-13 08:19:15]]></newsletter_date_add>
    <ip_registration_newsletter></ip_registration_newsletter>
    <last_passwd_gen><![CDATA[2014-06-20 16:56:30]]></last_passwd_gen>
    <secure_key><![CDATA[6a9b9eab95448d74a026b869d8cd723e]]></secure_key>
    <deleted><![CDATA[0]]></deleted>
    <passwd><![CDATA[6028853eb1033578f7432015042fa486]]></passwd>
    <lastname>newLastName</lastname>
    <firstname>newFirstName</firstname>
    <email><![CDATA[pub@prestashop.com]]></email>
    <id_gender><![CDATA[1]]></id_gender>
    <birthday><![CDATA[1970-01-15]]></birthday>
    <newsletter><![CDATA[1]]></newsletter>
    <optin><![CDATA[1]]></optin>
    <website></website>
    <company></company>
    <siret></siret>
    <ape></ape>
    <outstanding_allow_amount><![CDATA[0.000000]]></outstanding_allow_amount>
    <show_public_prices><![CDATA[0]]></show_public_prices>
    <id_risk><![CDATA[0]]></id_risk>
    <max_payment_days><![CDATA[0]]></max_payment_days>
    <active><![CDATA[1]]></active>
    <note></note>
    <is_guest><![CDATA[0]]></is_guest>
    <id_shop><![CDATA[1]]></id_shop>
    <id_shop_group><![CDATA[1]]></id_shop_group>
    <date_add><![CDATA[2014-08-01 13:20:37]]></date_add>
    <date_upd><![CDATA[2014-08-01 13:20:37]]></date_upd>
    <associations>
      <groups node_type="groups">
        <groups xlink:href="http://heatherfazelinia.com/api/groups/3">
          <id><![CDATA[3]]></id>
        </groups>
      </groups>
    </associations>
  </customer>
</prestashop>

该文件保存为EditedXML.xml。再次,根据文档(我上面粘贴),编辑资源我应该使用PUT将XML放回到相同的URL(即/ customers / 1)。所以我在创建此主题之前正在使用以下代码来尝试这样做:

        // PUT call
        var putRequest = new RestRequest("/customers/1", Method.PUT);
        var body = System.IO.File.ReadAllText("EditedXML.xml");
        request.AddBody(body);
        IRestResponse putResponse = client.Execute(putRequest);
        Console.WriteLine("Response: " + putResponse.Content);

现在出现了我的问题。我收到错误(最初是HTML格式,我把它打开为HTML,发布它更具可读性)。

Method Not Implemented

GET to /api/customers/1 not supported.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

我发现这个错误非常混乱有两个原因:

1)似乎即使我的请求是Method.PUT,它也被读作GET?

2)它声称的甚至不是真的?我必须在同一资源上调用GET函数来获取初始数据吗?

只要有人想看到GET电话,就在这里:

        request = new RestRequest(Method.GET);
        request.Resource = "/customers/1";
        IRestResponse<customer> newResponse = client.Execute<customer>(request);

任何人都知道发生了什么事?我不确定如何调试这个,我不确定PUT调用是否正常工作,或者我的PUT调用的参数是错误的,还是什么......

2 个答案:

答案 0 :(得分:0)

“request.AddBody(体);”似乎不起作用。

请查看此示例,了解我如何更新客户。

        // GET customer with id 1
        var client = new RestClient(PrestaShopBaseUrl);
        client.Authenticator = new HttpBasicAuthenticator(PrestaShopAccount, "");
        RestRequest request = new RestRequest("/customers/1", Method.GET);
        IRestResponse response = client.Execute(request);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(response.Content);
        doc.Save(@"Customer.xml");
        // do something with customer file

        // init XMLDocument and load customer in it
        doc = new XmlDocument();
        doc.Load(@"Customer.xml");
        // Update (PUT) customer
        request = new RestRequest("/customers/1", Method.PUT);
        request.Parameters.Clear();
        request.AddParameter("text/xml;charset=utf-8", doc.InnerXml, ParameterType.RequestBody);
        response = client.Execute(request);

答案 1 :(得分:0)

我们遇到了类似的问题,我们必须使用以下代码正确设置正文。

request.AddParameter("application/x-www-form-urlencoded", rawXml, ParameterType.RequestBody);