Jump to content

Problem with adding products via REST


portals

Recommended Posts

Hello

I am trying to create simple GUI in Python3 to adding product to shop.

This is a part of code:

    def send(self):

        out = self.request('api/products?schema=blank', self.api_key.text(), 0)

        empty_xml = ET.fromstring(out)
        
        xml = self.InsertIntoXML(empty_xml, "test", 0)
        #xml = dump(xml)

        xml = ET.tostring(xml, encoding='utf8', method='xml')
        print(xml)
        out = self.request('products', self.api_key.text(), xml)
        print (out)

def request(self, url, key, POST):
        username = 'kluczyk'
        password = ''
        api_root = 'http://192.168.1.43/api/'
        
        passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        passman.add_password(None, api_root, username, password)
        authhandler = urllib.request.HTTPBasicAuthHandler(passman)
        opener = urllib.request.build_opener(authhandler)
        urllib.request.install_opener(opener)
        
        if POST == 0:
            url = api_root + url
            response = urllib.request.urlopen(url).read()
        else:
            url = api_root + url
            params = urllib.parse.urlencode({'XML': POST})
            params = params.encode('utf-8')
            try:
                response = urllib.request.urlopen(url, params).read()
            except HTTPError as e:
                response = e.read()

        
        return response
 
Unfortunately I always gets this answer:
 

b'<?xml version="1.0" encoding="UTF-8"?>\n<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">\n<errors>\n<error>\n<code><![CDATA[127]]></code>\n<message><![CDATA[XML error : String could not be parsed as XML\nXML length : 3379\nOriginal XML : XML=%3C%3Fxml+version%3D%271.0%27+encoding%3D%27utf8%27%3F%3E%0A%3Cprestashop%3E%0A%3Cproduct%3E%0A%09%3Cid+%2F%3E%0A%09%3Cid_manufacturer+%2F%3E%0A%09%3Cid_supplier+%2F%3E%0A%09%3Cid_category_default+%2F%3E%0A%09%3Cnew+%2F%3E%0A%09%3Ccache_default_attribute+%2F%3E%0A%09%3Cid_default_image+%2F%3E%0A%09%3Cid_default_combination+%2F%3E%0A%09%3Cid_tax_rules_group+%2F%3E%0A%09%3Ctype+%2F%3E%0A%09%3Cid_shop_default+%2F%3E%0A%09%3Creference+%2F%3E%0A%09%3Csupplier_reference+%2F%3E%0A%09%3Clocation+%2F%3E%0A%09%3Cwidth+%2F%3E%0A%09%3Cheight+%2F%3E%0A%09%3Cdepth+%2F%3E%0A%09%3Cweight+%2F%3E%0A%09%3Cquantity_discount+%2F%3E%0A%09%3Cean13+%2F%3E%0A%09%3Cupc+%2F%3E%0A%09%3Ccache_is_pack+%2F%3E%0A%09%3Ccache_has_attachments+%2F%3E%0A%09%3Cis_virtual+%2F%3E%0A%09%3Con_sale+%2F%3E%0A%09%3Conline_only+%2F%3E%0A%09%3Cecotax+%2F%3E%0A%09%3Cminimal_quantity+%2F%3E%0A%09%3Cprice+%2F%3E%0A%09%3Cwholesale_price+%2F%3E%0A%09%3Cunity+%2F%3E%0A%09%3Cunit_price_ratio+%2F%3E%0A%09%3Cadditional_shipping_cost+%2F%3E%0A%09%3Ccustomizable+%2F%3E%0A%09%3Ctext_fields+%2F%3E%0A%09%3Cuploadable_files+%2F%3E%0A%09%3Cactive+%2F%3E%0A%09%3Credirect_type+%2F%3E%0A%09%3Cid_product_redirected+%2F%3E%0A%09%3Cavailable_for_order+%2F%3E%0A%09%3Cavailable_date+%2F%3E%0A%09%3Ccondition+%2F%3E%0A%09%3Cshow_price+%2F%3E%0A%09%3Cindexed+%2F%3E%0A%09%3Cvisibility+%2F%3E%0A%09%3Cadvanced_stock_management+%2F%3E%0A%09%3Cdate_add+%2F%3E%0A%09%3Cdate_upd+%2F%3E%0A%09%3Cmeta_description%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Fmeta_description%3E%0A%09%3Cmeta_keywords%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Fmeta_keywords%3E%0A%09%3Cmeta_title%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Fmeta_title%3E%0A%09%3Clink_rewrite%3E%3Clanguage+id%3D%221%22%3E%2528nowa%2529PS3%3C%2Flanguage%3E%3C%2Flink_rewrite%3E%0A%09%3Cname%3E%3Clanguage+id%3D%221%22%3E%28nowa%29%3C%2Flanguage%3E%3C%2Fname%3E%0A%09%3Cdescription%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Fdescription%3E%0A%09%3Cdescription_short%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Fdescription_short%3E%0A%09%3Cavailable_now%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Favailable_now%3E%0A%09%3Cavailable_later%3E%3Clanguage+id%3D%221%22+%2F%3E%3C%2Favailable_later%3E%0A%3Cassociations%3E%0A%3Ccategories%3E%0A%09%3Ccategory%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Fcategory%3E%0A%3C%2Fcategories%3E%0A%3Cimages%3E%0A%09%3Cimage%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Fimage%3E%0A%3C%2Fimages%3E%0A%3Ccombinations%3E%0A%09%3Ccombinations%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Fcombinations%3E%0A%3C%2Fcombinations%3E%0A%3Cproduct_option_values%3E%0A%09%3Cproduct_options_values%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Fproduct_options_values%3E%0A%3C%2Fproduct_option_values%3E%0A%3Cproduct_features%3E%0A%09%3Cproduct_feature%3E%0A%09%3Cid+%2F%3E%0A%09%3Ccustom+%2F%3E%0A%09%3Cid_feature_value+%2F%3E%0A%09%3C%2Fproduct_feature%3E%0A%3C%2Fproduct_features%3E%0A%3Ctags%3E%0A%09%3Ctag%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Ftag%3E%0A%3C%2Ftags%3E%0A%3Cstock_availables%3E%0A%09%3Cstock_available%3E%0A%09%3Cid+%2F%3E%0A%09%3Cid_product_attribute+%2F%3E%0A%09%3C%2Fstock_available%3E%0A%3C%2Fstock_availables%3E%0A%3Caccessories%3E%0A%09%3Cproduct%3E%0A%09%3Cid+%2F%3E%0A%09%3C%2Fproduct%3E%0A%3C%2Faccessories%3E%0A%3Cproduct_bundle%3E%0A%09%3Cproducts%3E%0A%09%3Cid+%2F%3E%0A%09%3Cquantity+%2F%3E%0A%09%3C%2Fproducts%3E%0A%3C%2Fproduct_bundle%3E%0A%3C%2Fassociations%3E%0A%3C%2Fproduct%3E%0A%3C%2Fprestashop%3E]]></message>\n</error>\n</errors>\n</prestashop>\n'


I'm trying to fix this for a couple of days. Please help me what I did wrong?

 

I made a simple php script to my app:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$xml = simplexml_load_string($_REQUEST['XML']);
var_dump($xml);

and it can parse a XML. Why Prestashop cannot?

Edited by portals (see edit history)
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...