Jump to content

C# - How send a new Product?


rddiniz

Recommended Posts

Describe, what do you mean? You can write some class on C# which sending data via Prestashop API, for example. Or class which prepare CSV file for Prestashop Import...

 

I want create a C# class. My simple code:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weitecnolog.com/prestatest/api/products");
            request.Credentials = new NetworkCredential(Constants.Prestashop.API_TOKEN, Constants.Prestashop.API_PASSWORD);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(xml);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

In this moment response = (HttpWebResponse)request.GetResponse(); throw a exception.

 

XML value:

<?xml version="1.0" encoding="utf-8"?><prestashop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><product><type notFilterable="true">simple</type><reference>Linha do Produto</reference><supplier_reference>Bolacha</supplier_reference><ean13>7896022056261</ean13><price>1.50</price><customizable>0</customizable><active>1</active><redirect_type>404</redirect_type><available_for_order>1</available_for_order><condition>new</condition><show_price>1</show_price><visibility>both</visibility><date_add>2016-08-11 15:40:57</date_add><date_upd>2016-08-11 15:40:57</date_upd><name><language id="1">WAFER 145GR CHOCOLATE C/AVELA blá blá blá</language></name><description><language id="1">WAFER 145GR CHOCOLATE C/AVELA</language></description><description_short><language id="1">WAFER 145GR CHOCOLATE C</language></description_short></product></prestashop>
Link to comment
Share on other sites

I have created a whole python library using prestapyt. I intend to share it soon enough (in the next week or so). I'm not sure if that's helpful, but in case it is, i decided to share.

 

WARNING : It's working but it's not so fast..

Interesting. But I'm not sure if I can integrate in my C # project. Can you tell me if this is possible?
 
I'm noob in this case... hehehe
 
I'm noob in this case
Link to comment
Share on other sites

 

I want create a C# class. My simple code:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weitecnolog.com/prestatest/api/products");
            request.Credentials = new NetworkCredential(Constants.Prestashop.API_TOKEN, Constants.Prestashop.API_PASSWORD);
            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(xml);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            HttpWebResponse response;
            response = (HttpWebResponse)request.GetResponse();

In this moment response = (HttpWebResponse)request.GetResponse(); throw a exception.

 

XML value:

<?xml version="1.0" encoding="utf-8"?><prestashop xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><product><type notFilterable="true">simple</type><reference>Linha do Produto</reference><supplier_reference>Bolacha</supplier_reference><ean13>7896022056261</ean13><price>1.50</price><customizable>0</customizable><active>1</active><redirect_type>404</redirect_type><available_for_order>1</available_for_order><condition>new</condition><show_price>1</show_price><visibility>both</visibility><date_add>2016-08-11 15:40:57</date_add><date_upd>2016-08-11 15:40:57</date_upd><name><language id="1">WAFER 145GR CHOCOLATE C/AVELA blá blá blá</language></name><description><language id="1">WAFER 145GR CHOCOLATE C/AVELA</language></description><description_short><language id="1">WAFER 145GR CHOCOLATE C</language></description_short></product></prestashop>

1. Try to Catch Exception and see what the problem.

2. bytes = System.Text.Encoding.ASCII.GetBytes(xml); - why ASCII encoding? Maybe using UTF8 will be better?

3. ContentLength = bytes.Length - Length in UTF8 different then ASCII, response can throw error in this case...

4. If xlm response return successful - maybe you need use some TimeOut before you getting response. Just try...

Link to comment
Share on other sites

1. Try to Catch Exception and see what the problem.

2. bytes = System.Text.Encoding.ASCII.GetBytes(xml); - why ASCII encoding? Maybe using UTF8 will be better?

3. ContentLength = bytes.Length - Length in UTF8 different then ASCII, response can throw error in this case...

4. If xlm response return successful - maybe you need use some TimeOut before you getting response. Just try...

Thank's!

 

I try!.

Link to comment
Share on other sites

Thank's!

 

I try!.

 

1. Try to Catch Exception and see what the problem.

2. bytes = System.Text.Encoding.ASCII.GetBytes(xml); - why ASCII encoding? Maybe using UTF8 will be better?

3. ContentLength = bytes.Length - Length in UTF8 different then ASCII, response can throw error in this case...

4. If xlm response return successful - maybe you need use some TimeOut before you getting response. Just try...

 

I try.

 

Error:

The remote server returned an error: (400) Bad Request.

 

 

The remote server returned an error: (400) Bad Request.

Link to comment
Share on other sites


Hi!

 

Working!!!

 

I did using code below:

 



                string authInfo = Constants.Prestashop.API_TOKEN + ":" + Constants.Prestashop.API_PASSWORD;
                authInfo = Convert.ToBase64String(Encoding.UTF8.GetBytes(authInfo));


                var client = new RestClient(Constants.Prestashop.API_URL);
                var request = new RestRequest(Method.POST);
                request.AddHeader("iwti-token", Constants.Prestashop.API_TOKEN);
                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("authorization", string.Format("Basic {0}", authInfo));
                request.AddParameter("undefined", xml, ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);


 

Add RestSharp reference in the project.

 

** The Date Format in XML is: "yyyy-MM-dd hh:mm:ss" 

Edited by rddiniz (see edit history)
  • Like 1
Link to comment
Share on other sites

Your Request on C# is not similar to Request needed for Presta API.

Try compare: what you send with what Presta required. Compare this in String format...

 
Alex,
 
With the way I post above is running ok, now.
 
The Presta creating a new product...
Link to comment
Share on other sites

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...