Updating order status in Prestashop via webservice api

佐手、 提交于 2019-12-07 22:50:14

问题


What am I doing wrong here?

First I make a request to get an existing order and I change the value of current_status field in retrieved xml. Then I make a PUT request with modified xml as a parameter but I get something like this in response:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[127]]></code>
<message><![CDATA[XML error : String could not be parsed as XML
XML length : 2864
Original XML : xml=%3C%3Fxml+version%3D%221%2E0%22+encoding...%3C%2Fprestashop%3E%0A]]></message>
</error>
</errors>
</prestashop>

When I debug my code there is no problem with xml parameter in the PUT request before ececution. Whay does then 'Original XML' show xml ecoded like so? Do I have to set some kind of encoding? My code is in C#.


回答1:


It seems that xml parameter has to be of type RequestBody. If not specified it is automatically set to type GetOrPost which is causing 'String could not be parsed as XML' error. I'm not sure how everything works but it seems that this is the solution to the problem.

RestRequest request;
request = new RestRequest("api/orders/" + orderID, Method.GET);
IRestResponse response = client.Execute(request);

XElement orderXML = XElement.Parse(response.Content);
XElement orderEl = orderXML.Descendants().FirstOrDefault();
orderEl.Element("current_state").Value = "10";    

request = new RestRequest("api/orders", Method.PUT);
request.AddParameter("xml", orderXML.ToString(), ParameterType.RequestBody);
IRestResponse response2 = client.Execute(request);



回答2:


You can also use a .Net wrapper written by C# instead of sending and retrieving XML requests. It also let you upload and change images which is a bit tricky using plain XML and http requests. It is called PrestaSharp.



来源:https://stackoverflow.com/questions/18489152/updating-order-status-in-prestashop-via-webservice-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!