Hello, we are developing an integration solution fo a customer, a backend module to synch with prestashop, I have problems consuming post request to create and update products.
Retrieving is just fine, but I'm unable to create or update, I'm using the same xml arriving from the product "get" request, modifying just the name and resending back as a post but I receive "BAd request" 400 errors, this is my code, do you have any advices?
target is something like "products/2" for an update or "products" for a create
I've seen that I get lots of CDATAs when retrieving a product, I've tried both sending xml with and without CDATA without any success..
doc.Outerxml should be exactly the xml I'm getting from a get request without id in case of create or with just a changed name in case of update. (I get that with a string = doc.LoadXml(responsePayload))
Thanks!
public string psPost(string target, XmlDocument doc)
{
Uri address = new Uri(baseurl + target);
HttpWebRequest request = null;
NetworkCredential cred = new NetworkCredential(credential, "");
// Create the web request
request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Credentials = new NetworkCredential(credential,"");
// Create the data we want to send.
string context = doc.OuterXml;
StringBuilder data = new StringBuilder();
data.Append("xml=" + 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
Stream postStream = request.GetRequestStream();
postStream.Write(byteData, 0, byteData.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (responseStream);
// Read the content.
return reader.ReadToEnd ();
}