Jump to content

Rest services and c# .Net


Recommended Posts

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 ();
 }

Link to comment
Share on other sites

Answering to myself, to troubleshoot rest services issues enable php errorors:

 

in config/config.inc.php

modify "@ini_set('display_errors', 'off')"

to "@ini_set('display_errors', 'on')"

 

then add something like this to the previous code to read the response (I had bad requests beacuse I tried to write into read only fields)

 

also remember:

update = PUT = use full object url (eg: ...api/products/1) = plain xml with heading

create = POST = object root (eg: ...api/products) = add "xml=" before the xml with heading

 

the easiest way to know what xml to send is getting the schema (eg: ...api/products?schema=blank)

 

	   postStream.Write(byteData, 0, byteData.Length);
	   HttpWebResponse response = null;

	   try
	   {
		    response = (HttpWebResponse)request.GetResponse();
	   }
	   catch (System.Net.WebException ex)
	   {

		   response = (HttpWebResponse)ex.Response;
	   }
	    Stream responseStream =  response.GetResponseStream();

					  // Open the stream using a StreamReader for easy access.
					  StreamReader reader = new StreamReader (responseStream);
					  // Read the content.
					  Console.WriteLine( reader.ReadToEnd ());

Link to comment
Share on other sites

  • 7 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...