Jump to content

Edit History

ozcandl333

ozcandl333

Retrieving and updating works fine via httpRequest.
But posting or using the library to add products fails.

Prestashop on:
Php version 7.3
Prestashop version 1.7.6.8

My API on:
.NetCore 3.1
PrestaSharp 1.2.8

In c# .Net core im trying to create products based on a list that i retrieve from somewhere else.

I tried the following:

(1) using PrestaSharp lib with existing product:
var product = ProductFactory.Get(24);
product .name[0].Value = "blabla";
ProductFactory.Add(product);

Getting error: Nullable object must have a value

(2) using PrestaSharp lib with new product:
var product= new Bukimedia.PrestaSharp.Entities.product();
product.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language() { id = 1, Value = "SomeName" });
product= ProductFactory.Add(product);

Getting error: Nullable object must have a value

(3) using XML with HttpRequests:

var blankProductSchema = myApi.GetResponse("products?schema=blank", RestSharp.Method.GET).Content;
var doc = new XmlDocument();
doc.LoadXml(blankProductSchema);

XmlNode root = doc.DocumentElement;
XmlNode prodNode = root.SelectSingleNode("product");
XmlNode name = prodNode.SelectSingleNode("name");
XmlNode language = name.SelectSingleNode("language");
language.InnerText = rimbaProdConverted.Product.Name.Language[0].Text;

var result = myApi.GetResponse("products", RestSharp.Method.POST, root.OuterXml);
result.conent =
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product id="24" xlink:href="https://thetoydoc.com/api/products/24"/>
</products>
</prestashop>

ResponseStatus: Competed
ResponseCode: OK

It did not add it, this is the product i created in the backoffice.

My getResponse method is:
 

public RestSharp.IRestResponse GetResponse(string endpoint, Method method, string xml = null)
        {
            var baseUrl = "http://www.thetoydoc.com/api/";
            var wsKey = "&ws_key=APIKEYAPIKEYAPIKEYAPIKEYAPIKEY";
            var client = new RestClient(baseUrl + endpoint + wsKey);
            client.Timeout = -1;

            var request = new RestRequest(method);
            if (xml != null && (method == Method.PUT || method == Method.POST))
            {
                request.AddHeader("Accept", "*/*");
                request.AddHeader("Content-Type", "application/xml");
                request.AddParameter("application/xml", xml, ParameterType.RequestBody);
                request.AddXmlBody(xml);
            }

            var result = client.Execute(request);
            return result;
        }


What am i doing wrong, why can't i create an product via my API?

ozcandl333

ozcandl333

Retrieving and updating works fine via httpRequest.
But posting or using the library to add products fails.

Prestashop on:
Php version 7.2
Prestashop version 1.7.6.8

My API on:
.NetCore 3.1
PrestaSharp 1.2.8

In c# .Net core im trying to create products based on a list that i retrieve from somewhere else.

I tried the following:

(1) using PrestaSharp lib with existing product:
var product = ProductFactory.Get(24);
product .name[0].Value = "blabla";
ProductFactory.Add(product);

Getting error: Nullable object must have a value

(2) using PrestaSharp lib with new product:
var product= new Bukimedia.PrestaSharp.Entities.product();
product.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language() { id = 1, Value = "SomeName" });
product= ProductFactory.Add(product);

Getting error: Nullable object must have a value

(3) using XML with HttpRequests:

var blankProductSchema = myApi.GetResponse("products?schema=blank", RestSharp.Method.GET).Content;
var doc = new XmlDocument();
doc.LoadXml(blankProductSchema);

XmlNode root = doc.DocumentElement;
XmlNode prodNode = root.SelectSingleNode("product");
XmlNode name = prodNode.SelectSingleNode("name");
XmlNode language = name.SelectSingleNode("language");
language.InnerText = rimbaProdConverted.Product.Name.Language[0].Text;

var result = myApi.GetResponse("products", RestSharp.Method.POST, root.OuterXml);
result.conent =
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product id="24" xlink:href="https://thetoydoc.com/api/products/24"/>
</products>
</prestashop>

ResponseStatus: Competed
ResponseCode: OK

It did not add it, this is the product i created in the backoffice.

My getResponse method is:
 

public RestSharp.IRestResponse GetResponse(string endpoint, Method method, string xml = null)
        {
            var baseUrl = "http://www.thetoydoc.com/api/";
            var wsKey = "&ws_key=APIKEYAPIKEYAPIKEYAPIKEYAPIKEY";
            var client = new RestClient(baseUrl + endpoint + wsKey);
            client.Timeout = -1;

            var request = new RestRequest(method);
            if (xml != null && (method == Method.PUT || method == Method.POST))
            {
                request.AddHeader("Accept", "*/*");
                request.AddHeader("Content-Type", "application/xml");
                request.AddParameter("application/xml", xml, ParameterType.RequestBody);
                request.AddXmlBody(xml);
            }

            var result = client.Execute(request);
            return result;
        }


What am i doing wrong, why can't i create an product via my API?

×
×
  • Create New...