Jump to content

Creating product fails via httpRequest and Prestasharp


ozcandl333
 Share

Recommended Posts

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?

Edited by ozcandl333 (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

  • 1 month later...

Based on the next i get this error in the catch


var prody = new Bukimedia.PrestaSharp.Entities.product();
                        prody.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language() { id = 1, Value = "SomeName" });
                        decimal.TryParse(rimbaProdConverted.Product.Price, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal resultPrice);
                        prody.price = resultPrice;
                        prody = ProductFactory.Add(prody);


Message:
Nullable object must have a value

Stacktrace:
at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
   at System.Nullable`1.get_Value()
   at Bukimedia.PrestaSharp.Factories.GenericFactory`1.Add(T Entity)
   at LoveSensezLib.Mapper.SyncPricesFromRimba() in D:\projects\thetoydoc\LoveSensezLib\Mapper.cs:line 139

Share this post


Link to post
Share on other sites

  • 3 weeks later...

Found out what the problem was.

I'm using PrestaSharp now and it works fine.
I needed to change the rewritings in my .htaccess file. The url in my .htaccess where redirected to my site which included www in the url.
So all the Get and update was working, but the post was beeing redirect to a GET. ( real weird )

Share this post


Link to post
Share on other sites

  • 2 months later...
On 12/29/2020 at 9:54 AM, ozcandl333 said:

Found out what the problem was.

I'm using PrestaSharp now and it works fine.
I needed to change the rewritings in my .htaccess file. The url in my .htaccess where redirected to my site which included www in the url.
So all the Get and update was working, but the post was beeing redirect to a GET. ( real weird )

PLEASE PLEASE PLEASE, it's a month I try to "get a post" (pun intended), could you write your .htaccess? I have the same problem, I have Get in the logs when I try to use  POST.

PLEASE PLEASE HELP!

MY HTACCESS: 

 

# ~~start~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again
# .htaccess automaticaly generated by PrestaShop e-commerce open-source solution
# http://www.prestashop.com - http://www.prestashop.com/forums

<IfModule mod_rewrite.c>
<IfModule mod_env.c>
SetEnv HTTP_MOD_REWRITE On
</IfModule>

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule . - [E=HTTP_AUTHORIZATION:%1]

#Domain: crazy4japan.com
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule . - [E=REWRITEBASE:/]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^api(?:/(.*))?$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]

# Images
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$1$2$3$4.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$1$2$3$4$5.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2$3.jpg [L]
RewriteCond %{HTTP_HOST} ^crazy4japan.com$
RewriteRule ^c/([a-zA-Z_-]+)(-[0-9]+)?/.+\.jpg$ %{ENV:REWRITEBASE}img/c/$1$2.jpg [L]
# AlphaImageLoader for IE and fancybox
RewriteRule ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 [L]

# Dispatcher
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ %{ENV:REWRITEBASE}index.php [NC,L]
</IfModule>

AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType application/font-woff .woff
AddType font/woff2 .woff2
<IfModule mod_headers.c>
    <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|svg)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>

    <FilesMatch "\.pdf$">
      Header set Content-Disposition "Attachment"
      Header set X-Content-Type-Options "nosniff"
    </FilesMatch>
</IfModule>

<Files composer.lock>
    # Apache 2.2
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>

    # Apache 2.4
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</Files>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/javascript "access plus 1 week"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-javascript "access plus 1 week"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/vnd.microsoft.icon "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/x-font-woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType font/opentype "access plus 1 year"
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
    ExpiresByType application/x-font-otf "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
    Header unset Etag
</IfModule>
FileETag none
<IfModule mod_deflate.c>
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript application/x-javascript font/ttf application/x-font-ttf font/otf application/x-font-otf font/opentype image/svg+xml
    </IfModule>
</IfModule>

#If rewrite mod isn't enabled
ErrorDocument 404 /index.php?controller=404

# ~~end~~ Do not remove this comment, Prestashop will keep automatically the code outside this comment when .htaccess will be generated again

## SITE REFERRAL BLOCK


RewriteCond %{HTTP_REFERER} [email protected]\.com [NC,OR]

RewriteRule .* – [F]

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More