简体   繁体   English

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

[英]RestSharp PUT XML, RestSharp is sending it as GET?

I am attempting to edit a product using the prestashop API, using RestSharp in C#, using XML. 我试图使用prestashop API,使用C#中的RestSharp和XML来编辑产品。 The documentation's instructions are as follows: 该文档的说明如下:

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.

I am attempting to edit /customers/1. 我正在尝试编辑/ customers / 1。

My GET calls are working fine to retrieve the data. 我的GET调用可以正常工作以检索数据。 I am now deserializing the data, editing it as needed, and re-saving to an XML file. 我现在正在反序列化数据,根据需要对其进行编辑,然后重新保存为XML文件。 All seems to be going well. 一切似乎进展顺利。 The only fields I am attempting to change right now are firstname and lastname. 我现在尝试更改的唯一字段是名字和姓氏。 The rest of the data is untouched. 其余数据保持不变。 Here is a copy of the XML I am using: 这是我正在使用的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>

That file is saved as EditedXML.xml. 该文件另存为EditedXML.xml。 Again, according to the documentation (that I pasted above), to edit a resource I am supposed to use PUT to put the XML back to the same URL (which is /customers/1). 同样,根据文档(我在上面粘贴),要编辑资源,我应该使用PUT将XML放回相同的URL(/ customers / 1)。 So I am using the following code right before creating this topic to try to do just that: 因此,在创建此主题之前,我正在使用以下代码来尝试做到这一点:

        // 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);

Now comes my problem. 现在是我的问题。 I am getting the error (originally in HTML form, I opened it as HTML to post it more readable:) 我收到错误消息(最初是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.

That error I find VERY confusing for 2 reasons: 我发现该错误非常令人困惑,原因有两个:

1) It seems that even though my request is Method.PUT, it is being read as GET? 1)看来,即使我的请求是Method.PUT,它仍被读取为GET?

2) What it is claiming isn't even true? 2)它声称的说法不正确? I have to call a GET function on that same resource to get the initial data? 我必须在同一资源上调用GET函数以获取初始数据吗?

Just incase anyone would like to see the GET call, here it is: 以防万一有人希望看到GET调用,这里是:

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

Anyone have any idea what is going on? 有人知道发生了什么吗? I'm not sure how to debug this, I'm not sure if the PUT call is working at all, or if the arguments with my PUT call are wrong, or what... 我不确定该如何调试,也不知道PUT调用是否可以正常工作,或者我的PUT调用的参数是否错误,或者什么...

我们有一个类似的问题,我们必须使用以下代码正确设置主体。

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

"request.AddBody(body);" “ request.AddBody(body);” seems not to be working. 似乎没有用。

Please check this example on how i'm updating customer. 请检查此示例以了解我如何更新客户。

        // 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM