Jump to content

API Post Prestashop in python


anormah

Recommended Posts

import requests
import xml.etree.ElementTree as ET

# Connection parameters to the webservice
api_url = 'http://localhost/api'
api_key = 'my key'

# Resource to retrieve the empty schema
resource = 'products'
schema_param = 'schema=blank'

# Build the URL for the GET request to the empty schema
url = f'{api_url}/{resource}?{schema_param}&ws_key={api_key}'

# Send the GET request to retrieve the empty schema
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    # Load the XML content from the response
    xml_content = response.text

    # Parse the XML content
    root = ET.fromstring(xml_content)

    # Find the "name" element with language ID 1
    name_element = root.find(".//name/language[@id='1']")
    # Find the "price" element
    price_element = root.find(".//price")

    # Add content to the "name" element
    name_element.text = "apexxxxxlgenddssssss"
    # Add content to the "price" element
    price_element.text = "19.99"

    # Convert the XML tree to a string
    modified_xml = ET.tostring(root, encoding="unicode")

    # Convert the string to UTF-8 encoding
    modified_xml = modified_xml.encode('utf-8')

    # Display the modified XML content
    print(modified_xml.decode('utf-8'))

    # URL to create a new product
    create_url = f'{api_url}/{resource}?ws_key={api_key}'

    # Send the POST request with the modified data
    response = requests.post(create_url, data=modified_xml, headers={'Content-Type': 'application/xml'})

    # Check if the request was successful
    if response.status_code == 201:
        # Display success message
        print("The new product has been created successfully.")
    else:
        # Display error message if the request failed
        print(f"Error creating the product: {response.status_code}")
else:
    # Display error message if the request failed
    print(f"Request error: {response.status_code}")

i try to create a product name in my script "apexxxxxlgenddssssss" and price "19.99" but when i run script i get error 500. i check my log and i have nothing.
can anyone help me i'm novice sry for my english i try to see my log but nothing is writing i use apache in localhost and i'm in ubuntu

Link to comment
Share on other sites

  • 3 months later...
On 5/24/2023 at 9:55 PM, anormah said:

when i run script i get error 500

Hi,

A 500 Internal Server Error typically indicates that there's an issue on the server side of your PrestaShop installation.

Since you mentioned that you couldn't find any error messages in your logs, it might be helpful to enable error reporting and debugging to get more information about what's going wrong.

Edit the config/defines.inc.php file in your Prestashop root directory.

Look for the following lines in the file:

define('_PS_MODE_DEV_', false);
define('_PS_DEBUG_SQL_', false);

change them to

define('_PS_MODE_DEV_', true);
define('_PS_DEBUG_SQL_', true);

Enabling _PS_MODE_DEV_ and _PS_DEBUG_SQL_ will turn on error reporting and debugging mode.

Now, when you run your script and encounter a 500 Internal Server Error, PrestaShop should display more detailed error messages on the screen or in the server logs. This should help you identify the issue causing the error.

Let me know what error you get.

Thanks!

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