RestSharp PUT XML,RestSharp发送它作为得到什么? [英] RestSharp PUT XML, RestSharp is sending it as GET?

查看:369
本文介绍了RestSharp PUT XML,RestSharp发送它作为得到什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编辑使用的Prestashop API产品,在C#中使用RestSharp,使用XML。该文件的说明如下:

I am attempting to edit a product using the prestashop API, using RestSharp in C#, using 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.



我在尝试编辑/客户/ 1。

I am attempting to edit /customers/1.

我的GET调用做工精细检索数据。我现在反序列化数据,编辑它根据需要,并重新保存到一个XML文件。一切似乎很顺利。我试图马上改变字段只有名字和姓氏。该数据的其余部分是不变。下面是我使用的XML的副本:

My GET calls are working fine to retrieve the data. I am now deserializing the data, editing it as needed, and re-saving to an XML file. 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:

<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的资源(这是/客户/ 1)。所以我用下面的代码创建权这个话题之前,要尽量做到这一点:

That file is saved as 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). 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);

现在谈到我的问题。我收到错误(最初在HTML表单,我打开它作为HTML发布更具可读性:)

Now comes my problem. I am getting the error (originally in HTML form, I opened it as HTML to post it more readable:)

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)看来,尽管我的要求是Method.PUT,它被理解为得到什么?

1) It seems that even though my request is Method.PUT, it is being read as GET?

2),它声称什么是不是真的? ?我要呼吁相同的资源获取函数来获得的初始数据

2) What it is claiming isn't even true? 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:

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



任何人有任何想法是怎么回事?我不知道如何调试这个,我不知道,如果PUT调用工作可言,或者如果我PUT调用的参数都错了,还是什么......

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...

推荐答案

request.AddBody(身体);似乎不是工作。

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

这篇关于RestSharp PUT XML,RestSharp发送它作为得到什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆