RestSharp PUT XML,RestSharp将它作为GET发送?
我正在尝试使用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的副本:
newLastName newFirstName
该文件保存为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 newResponse = client.Execute(request);
任何人都知道发生了什么事吗? 我不确定如何调试这个,我不确定PUT调用是否正常工作,或者我的PUT调用的参数是错误的,还是什么……
我们遇到了类似的问题,我们必须使用以下代码来正确设置正文。
request.AddParameter("application/x-www-form-urlencoded", rawXml, ParameterType.RequestBody);
“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);