Jump to content

Edit History

Nep

Nep

I have a code that firstly read the cart rules and check if there is any duplicate code and this part works fine. I can get from the "GET" service.

However, when I move on to "POST", it keeps returning server status 200 to me. It seems it changed from POST to GET automatically. Is there any way to fix this?

I am also the admin to the prestashop. I have already created the webservice and enable the get and post function for the cart rules.
 

protected void CreateRecordPresta(string Coupon_Code, float Reduction_amount)
    {
        try
        {
            string PostURL = $"https://www.domain.com.xx/api/cart_rules&ws_key={WebService_LoginName}";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(PostURL);
            webRequest.Credentials = new NetworkCredential(WebService_LoginName, WebService_Password);
            webRequest.Method = "POST";
            //webRequest.ContentType = "application/xml";
            webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            string CurrentDate = DateTime.Now.ToString("yyyy-MM-dd");
            string CurrentTime = DateTime.Now.ToString("HH:mm:ss");
            string Current = CurrentDate + " " + CurrentTime;
            string DateEnd = DateTime.Now.AddMonths(2).ToString("yyyy-MM-dd");
            string description = "";
            string Coupon_no = Coupon_Code;
            float reduction_amount = Reduction_amount;
            //int Customer_id = 4641;
            //Create POST data and convert it to a byte array.
            String PostData = "<?xml version='1.0' encoding='UTF-8'?>" +
                "<prestashop xmlns:xlink=\"http://www.w3.org/1999/xlink\">" +
                "<cart_rule>" +
                //$"<id_customer><![CDATA[{Customer_id}]]></id_customer>" +
                $"<date_from><![CDATA[{Current}]]></date_from>" +
                $"<date_to><![CDATA[{DateEnd} 00:00:00]]></date_to>" +
                $"<description><![CDATA[{description}]]></description>" +
                "<quantity><![CDATA[1]]></quantity>" +
                "<quantity_per_user><![CDATA[1]]></quantity_per_user>" +
                "<priority><![CDATA[1]]></priority>" +
                //"< partial_use >< ![CDATA[0]] ></ partial_use >" +
                $"<code><![CDATA[{Coupon_no}]]></code>" +
                //"< minimum_amount >< ![CDATA[0.00]] ></ minimum_amount >"+
                //"< minimum_amount_tax >< ![CDATA[0]] ></ minimum_amount_tax >" +
                "<minimum_amount_currency><![CDATA[1]]></minimum_amount_currency>" +
                //"< minimum_amount_shipping >< ![CDATA[0]] ></ minimum_amount_shipping >" +
                //"< country_restriction >< ![CDATA[0]] ></ country_restriction >" +
                //"< carrier_restriction >< ![CDATA[0]] ></ carrier_restriction >" +
                //"< group_restriction >< ![CDATA[0]] ></ group_restriction >" +
                "<cart_rule_restriction><![CDATA[1]]></cart_rule_restriction>" +
                "<product_restriction><![CDATA[1]]></product_restriction>" +
                //"< shop_restriction >< ![CDATA[0]] ></ shop_restriction >" +
                //"< free_shipping >< ![CDATA[0]] ></ free_shipping >" +
                //"< reduction_percent >< ![CDATA[0.00]] ></ reduction_percent >" +
                $"<reduction_amount><![CDATA[{reduction_amount}]]></reduction_amount>" +
                //"< reduction_tax >< ![CDATA[0]] ></ reduction_tax >" +
                ////"< reduction_currency >< ![CDATA[1]] ></ reduction_currency >" +
                //"< reduction_product >< ![CDATA[0]] ></ reduction_product >" +
                //"< reduction_exclude_special >< ![CDATA[0]] ></ reduction_exclude_special >" +
                //"< gift_product >< ![CDATA[0]] ></ gift_product >" +
                //"< gift_product_attribute >< ![CDATA[0]] ></ gift_product_attribute >" +
                //"< highlight >< ![CDATA[0]] ></ highlight >" +
                "<active><![CDATA[1]]></active>" +
                ////$"< date_add >< ![CDATA[{Current}]] ></ date_add >" +
                ////$"< date_upd >< ![CDATA[{Current}]] ></ date_upd >" +
                "<name>" +
                "<language id=\"1\" xlink:href=\"https://www.domain.com.xx/api/languages/1\">" +
                $"<![CDATA[Test]]>" +
                "</language>" +
                "<language id=\"2\" xlink:href=\"https://www.domain.com.xx/api/languages/2\">" +
                $"<![CDATA[Test]]>" +
                "</language>" +
                "</name>" +
                "</cart_rule>" +
                "</prestashop>";
            Byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
            //Set the ContentType property of the WebRequest.
            //webRequest.ContentType = "application/x-www-form-urlencoded"
            //Set the ContentLength property of the WebRequest.
            webRequest.ContentLength = byteArray.Length;
            //Get the request stream.
            Stream stream = webRequest.GetRequestStream();
            //Write the data to the request stream
            stream.Write(byteArray, 0, byteArray.Length);
            //Close the Stream object
            stream.Close();

            //Get the response
            HttpWebResponse responsepost = (HttpWebResponse)webRequest.GetResponse();
            //Display the status.
            System.Diagnostics.Debug.WriteLine(responsepost.StatusCode.ToString());//StatusCode
            System.Diagnostics.Debug.WriteLine(responsepost.StatusDescription.ToString());
            //Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            //Get the stream containing content returned by the server & Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(responsepost.GetResponseStream());
            //Read the content.
            String responseFromServer = reader.ReadToEnd();
            //Display the content.
            System.Diagnostics.Debug.WriteLine(responseFromServer);
            //Clean up the streams.
            reader.Close();
            stream.Close();
            responsepost.Close();

            return;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("UnkownError");
            alert.Text = Resources.Resource.Redeem_Error;
            alert.Visible = true;
            CreateButton.Enabled = true;
            return;
        }
    }

It returns sth like this
 

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<cart_rules>
<cart_rule id="4" xlink:href="https://domain.com.xx/api/cart_rules/4"/>
<cart_rule id="12" xlink:href="https://domain.com.xx/api/cart_rules/12"/>
<cart_rule id="14" xlink:href="https://domain.com.xx/api/cart_rules/14"/>
<cart_rule id="15" xlink:href="https://domain.com.xx/api/cart_rules/15"/>
</cart_rules>
</prestashop>

Is there anythings that I need to modify?

Nep

Nep

I have a code that firstly read the cart rules and check if there is any duplicate code and this part works fine. I can get from the "GET" service.

However, when I move on to "POST", it keeps returning server status 200 to me. It seems it changed from POST to GET automatically. Is there any way to fix this?

 

protected void CreateRecordPresta(string Coupon_Code, float Reduction_amount)
    {
        try
        {
            string PostURL = $"https://www.domain.com.xx/api/cart_rules&ws_key={WebService_LoginName}";
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(PostURL);
            webRequest.Credentials = new NetworkCredential(WebService_LoginName, WebService_Password);
            webRequest.Method = "POST";
            //webRequest.ContentType = "application/xml";
            webRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            string CurrentDate = DateTime.Now.ToString("yyyy-MM-dd");
            string CurrentTime = DateTime.Now.ToString("HH:mm:ss");
            string Current = CurrentDate + " " + CurrentTime;
            string DateEnd = DateTime.Now.AddMonths(2).ToString("yyyy-MM-dd");
            string description = "";
            string Coupon_no = Coupon_Code;
            float reduction_amount = Reduction_amount;
            //int Customer_id = 4641;
            //Create POST data and convert it to a byte array.
            String PostData = "<?xml version='1.0' encoding='UTF-8'?>" +
                "<prestashop xmlns:xlink=\"http://www.w3.org/1999/xlink\">" +
                "<cart_rule>" +
                //$"<id_customer><![CDATA[{Customer_id}]]></id_customer>" +
                $"<date_from><![CDATA[{Current}]]></date_from>" +
                $"<date_to><![CDATA[{DateEnd} 00:00:00]]></date_to>" +
                $"<description><![CDATA[{description}]]></description>" +
                "<quantity><![CDATA[1]]></quantity>" +
                "<quantity_per_user><![CDATA[1]]></quantity_per_user>" +
                "<priority><![CDATA[1]]></priority>" +
                //"< partial_use >< ![CDATA[0]] ></ partial_use >" +
                $"<code><![CDATA[{Coupon_no}]]></code>" +
                //"< minimum_amount >< ![CDATA[0.00]] ></ minimum_amount >"+
                //"< minimum_amount_tax >< ![CDATA[0]] ></ minimum_amount_tax >" +
                "<minimum_amount_currency><![CDATA[1]]></minimum_amount_currency>" +
                //"< minimum_amount_shipping >< ![CDATA[0]] ></ minimum_amount_shipping >" +
                //"< country_restriction >< ![CDATA[0]] ></ country_restriction >" +
                //"< carrier_restriction >< ![CDATA[0]] ></ carrier_restriction >" +
                //"< group_restriction >< ![CDATA[0]] ></ group_restriction >" +
                "<cart_rule_restriction><![CDATA[1]]></cart_rule_restriction>" +
                "<product_restriction><![CDATA[1]]></product_restriction>" +
                //"< shop_restriction >< ![CDATA[0]] ></ shop_restriction >" +
                //"< free_shipping >< ![CDATA[0]] ></ free_shipping >" +
                //"< reduction_percent >< ![CDATA[0.00]] ></ reduction_percent >" +
                $"<reduction_amount><![CDATA[{reduction_amount}]]></reduction_amount>" +
                //"< reduction_tax >< ![CDATA[0]] ></ reduction_tax >" +
                ////"< reduction_currency >< ![CDATA[1]] ></ reduction_currency >" +
                //"< reduction_product >< ![CDATA[0]] ></ reduction_product >" +
                //"< reduction_exclude_special >< ![CDATA[0]] ></ reduction_exclude_special >" +
                //"< gift_product >< ![CDATA[0]] ></ gift_product >" +
                //"< gift_product_attribute >< ![CDATA[0]] ></ gift_product_attribute >" +
                //"< highlight >< ![CDATA[0]] ></ highlight >" +
                "<active><![CDATA[1]]></active>" +
                ////$"< date_add >< ![CDATA[{Current}]] ></ date_add >" +
                ////$"< date_upd >< ![CDATA[{Current}]] ></ date_upd >" +
                "<name>" +
                "<language id=\"1\" xlink:href=\"https://www.domain.com.xx/api/languages/1\">" +
                $"<![CDATA[Test]]>" +
                "</language>" +
                "<language id=\"2\" xlink:href=\"https://www.domain.com.xx/api/languages/2\">" +
                $"<![CDATA[Test]]>" +
                "</language>" +
                "</name>" +
                "</cart_rule>" +
                "</prestashop>";
            Byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
            //Set the ContentType property of the WebRequest.
            //webRequest.ContentType = "application/x-www-form-urlencoded"
            //Set the ContentLength property of the WebRequest.
            webRequest.ContentLength = byteArray.Length;
            //Get the request stream.
            Stream stream = webRequest.GetRequestStream();
            //Write the data to the request stream
            stream.Write(byteArray, 0, byteArray.Length);
            //Close the Stream object
            stream.Close();

            //Get the response
            HttpWebResponse responsepost = (HttpWebResponse)webRequest.GetResponse();
            //Display the status.
            System.Diagnostics.Debug.WriteLine(responsepost.StatusCode.ToString());//StatusCode
            System.Diagnostics.Debug.WriteLine(responsepost.StatusDescription.ToString());
            //Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
            //Get the stream containing content returned by the server & Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(responsepost.GetResponseStream());
            //Read the content.
            String responseFromServer = reader.ReadToEnd();
            //Display the content.
            System.Diagnostics.Debug.WriteLine(responseFromServer);
            //Clean up the streams.
            reader.Close();
            stream.Close();
            responsepost.Close();

            return;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("UnkownError");
            alert.Text = Resources.Resource.Redeem_Error;
            alert.Visible = true;
            CreateButton.Enabled = true;
            return;
        }
    }

It returns sth like this
 

<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<cart_rules>
<cart_rule id="4" xlink:href="https://domain.com.xx/api/cart_rules/4"/>
<cart_rule id="12" xlink:href="https://domain.com.xx/api/cart_rules/12"/>
<cart_rule id="14" xlink:href="https://domain.com.xx/api/cart_rules/14"/>
<cart_rule id="15" xlink:href="https://domain.com.xx/api/cart_rules/15"/>
</cart_rules>
</prestashop>

Is there anythings that I need to modify?

×
×
  • Create New...