1

I could GET Xml data from the Prestashop api, but couldn't PUT/POST Xml data to PrestaShop API.

Could someone suggest where I might be going wrong?

public POST_xml()
{
     Uri address = new Uri("http://.../api/countries/1");
     HttpWebRequest request = WebRequest.Create("http://.../api/countries/1") as HttpWebRequest;

     NetworkCredential("15PJQ4V8CXI22JVW1TKZASDF0OAYNBLA", "");

     // Create the web request  
     request = WebRequest.Create(address) as HttpWebRequest;

     // Set type to POST  
     request.Method = "POST";
     request.ContentType = "application/x-www-form-urlencoded";

     // Create the data we want to send.  

     string context = "<prestashop><country><id>1</id><id_zone xlink:href=\"http://.../api/zones/1\">";
     context += "1</id_zone><id_currency/><iso_code>DE</iso_code><call_prefix>49</call_prefix><active>1</active><contains_states>0</contains_states><need_identification_number>0</need_identification_number><need_zip_code>1</need_zip_code><zip_code_format>NNNNN</zip_code_format><display_tax_label>1</display_tax_label><name><language";
     context += " id=\"6\" xlink:href=\"http://.../api/languages/6\">Germanyxx</language></name></country></prestashop>";


     StringBuilder data = new StringBuilder();
     data.Append("&context=" + HttpUtility.UrlEncode(context));


     // Create a byte array of the data we want to send  
     byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

     // Set the content length in the request headers  
     request.ContentLength = byteData.Length;

     // Write data  
     using (Stream postStream = request.GetRequestStream())
     {
         postStream.Write(byteData, 0, byteData.Length);
     }
}
PrestaShopDeveloper
  • 3,110
  • 3
  • 21
  • 30
kiyan r.z.h
  • 301
  • 4
  • 6

3 Answers3

2

I'm not export in c#, but I did make a REST call work both in Java and VBA. Few things are wrong with your code. I assume you would like to create a brand new country entry. Then your URL address should be

Uri address = new Uri("http://.../api/countries/");

And in your string context, you cannot set the ID for the new resource.

What you show here would pretty much work for a PUT request on the resource "countries/1".

j0k
  • 22,600
  • 28
  • 79
  • 90
1
  1. If you want to update country with id=1, you will want to set request type to "PUT"
  2. No need to append "&context"
  3. If that still not working. Download their example code at http://doc.prestashop.com/display/PS14/Using+the+REST+webservice. Then run the code "update.php", see how they make the request.
Ani
  • 41
  • 4
1
  1. PrestaShop webservice API expects the XML to be prefixed with xml= for POST requests.
  2. Use a program called Fiddler - Http Debugging proxy to see exactly what you are sending and what the webservice is replying.