Jump to content

WebService Prestashop - Error 400 updating a product


edkalel

Recommended Posts

Hi everybody,

 

I have implemented a function in Visual Basic .NET where I make a request to prestashop webservice to update a resource.

When I try to update a product, I get an error '400 bad request'.

I think the problem is the structure of the xml that I send to webservice. Previously to send the xml I have delete all the 'read-only' and 'not_filterable="true"' nodes.

 

The structure of the xml is the following:

<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <product>
      <id_manufacturer xlink:href="http://localhost/prestashop/api/manufacturers/1">1</id_manufacturer>
      <id_supplier xlink:href="http://localhost/prestashop/api/suppliers/1">1</id_supplier>
      <id_category_default xlink:href="http://localhost/prestashop/api/categories/5">5</id_category_default>
      <new>
      </new>
      <cache_default_attribute/>
      <id_tax_rules_group xlink:href="http://localhost/prestashop/api/tax_rule_groups/1">1</id_tax_rules_group>
      <reference>demo_1_modified</reference>
      <supplier_reference>
      </supplier_reference>
      <location>
      </location>
      <width>0.000000</width>
      <height>0.000000</height>
      <depth>0.000000</depth>
      <weight>0.000000</weight>
      <quantity_discount>0</quantity_discount>
      <ean13>0</ean13>
      <upc>
      </upc>
      <cache_is_pack>0</cache_is_pack>
      <cache_has_attachments>0</cache_has_attachments>
      <is_virtual>0</is_virtual>
      <on_sale>0</on_sale>
      <online_only>0</online_only>
      <ecotax>0.000000</ecotax>
      <minimal_quantity>1</minimal_quantity>
      <price>19.980000</price>
      <wholesale_price>4.950000</wholesale_price>
      <unit_price_ratio>0.000000</unit_price_ratio>
      <additional_shipping_cost>0.00</additional_shipping_cost>
      <customizable>0</customizable>
      <text_fields>0</text_fields>
      <uploadable_files>0</uploadable_files>
      <active>1</active>
      <redirect_type>404</redirect_type>
      <id_product_redirected>0</id_product_redirected>
      <available_for_order>1</available_for_order>
      <available_date>0000-00-00</available_date>
      <condition>new</condition>
      <show_price>1</show_price>
      <indexed>1</indexed>
      <visibility>both</visibility>
      <advanced_stock_management>0</advanced_stock_management>
      <meta_description>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"></language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"></language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"></language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"></language>
      </meta_description>
      <meta_keywords>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"></language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"></language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"></language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"></language>
      </meta_keywords>
      <meta_title>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"></language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"></language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"></language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"></language>
      </meta_title>
      <link_rewrite>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1">faded-short-sleeve-tshirts</language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2">faded-short-sleeve-tshirts</language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3">faded-short-sleeve-tshirts</language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4">faded-short-sleeve-tshirts</language>
      </link_rewrite>
      <name>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1">Faded Short Sleeve T-shirts</language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2">Faded Short Sleeve T-shirts</language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3">Faded Short Sleeve T-shirts</language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4">Faded Short Sleeve T-shirts</language>
      </name>
      <description>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"><p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>
      </language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"><p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>
      </language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"><p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>
      </language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"><p>Fashion has been creating well-designed collections since 2010. The brand offers feminine designs delivering stylish separates and statement dresses which has since evolved into a full ready-to-wear collection in which every item is a vital part of a woman's wardrobe. The result? Cool, easy, chic looks with youthful elegance and unmistakable signature style. All the beautiful pieces are made in Italy and manufactured with the greatest attention. Now Fashion extends to a range of accessories including shoes, hats, belts and more!</p>
      </language>
      </description>
      <description_short>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"><p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p></language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"><p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p></language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"><p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p></language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"><p>Faded short sleeve t-shirt with high neckline. Soft and stretchy material for a comfortable fit. Accessorize with a straw hat and you're ready for summer!</p></language>
      </description_short>
      <available_now>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1">In stock</language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2">In stock</language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3">In stock</language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4">In stock</language>
      </available_now>
      <available_later>
        <language id="1" xlink:href="http://localhost/prestashop/api/languages/1"></language>
        <language id="2" xlink:href="http://localhost/prestashop/api/languages/2"></language>
        <language id="3" xlink:href="http://localhost/prestashop/api/languages/3"></language>
        <language id="4" xlink:href="http://localhost/prestashop/api/languages/4"></language>
      </available_later>
      <associations>
        <categories node_type="categories">
          <categories xlink:href="http://localhost/prestashop/api/categories/2">
            <id>2</id>
          </categories>
          <categories xlink:href="http://localhost/prestashop/api/categories/3">
            <id>3</id>
          </categories>
          <categories xlink:href="http://localhost/prestashop/api/categories/4">
            <id>4</id>
          </categories>
          <categories xlink:href="http://localhost/prestashop/api/categories/5">
            <id>5</id>
          </categories>
        </categories>
        <images node_type="images">
          <images xlink:href="http://localhost/prestashop/api/images/products/1/1">
            <id>1</id>
          </images>
          <images xlink:href="http://localhost/prestashop/api/images/products/1/2">
            <id>2</id>
          </images>
          <images xlink:href="http://localhost/prestashop/api/images/products/1/3">
            <id>3</id>
          </images>
          <images xlink:href="http://localhost/prestashop/api/images/products/1/4">
            <id>4</id>
          </images>
        </images>
        <combinations node_type="combinations">
          <combinations xlink:href="http://localhost/prestashop/api/combinations/1">
            <id>1</id>
          </combinations>
          <combinations xlink:href="http://localhost/prestashop/api/combinations/2">
            <id>2</id>
          </combinations>
          <combinations xlink:href="http://localhost/prestashop/api/combinations/3">
            <id>3</id>
          </combinations>
          <combinations xlink:href="http://localhost/prestashop/api/combinations/4">
            <id>4</id>
          </combinations>
          <combinations xlink:href="http://localhost/prestashop/api/combinations/5">
            <id>5</id>
          </combinations>
          <combinations xlink:href="http://localhost/prestashop/api/combinations/6">
            <id>6</id>
          </combinations>
        </combinations>
        <product_option_values node_type="product_option_values">
          <product_option_values xlink:href="http://localhost/prestashop/api/product_option_values/1">
            <id>1</id>
          </product_option_values>
          <product_option_values xlink:href="http://localhost/prestashop/api/product_option_values/13">
            <id>13</id>
          </product_option_values>
          <product_option_values xlink:href="http://localhost/prestashop/api/product_option_values/14">
            <id>14</id>
          </product_option_values>
          <product_option_values xlink:href="http://localhost/prestashop/api/product_option_values/2">
            <id>2</id>
          </product_option_values>
          <product_option_values xlink:href="http://localhost/prestashop/api/product_option_values/3">
            <id>3</id>
          </product_option_values>
        </product_option_values>
        <product_features node_type="product_features">
          <product_features xlink:href="http://localhost/prestashop/api/product_features/5">
            <id>5</id>
            <custom>0</custom>
            <id_feature_value xlink:href="http://localhost/prestashop/api/product_feature_values/5">5</id_feature_value>
          </product_features>
          <product_features xlink:href="http://localhost/prestashop/api/product_features/6">
            <id>6</id>
            <custom>0</custom>
            <id_feature_value xlink:href="http://localhost/prestashop/api/product_feature_values/11">11</id_feature_value>
          </product_features>
          <product_features xlink:href="http://localhost/prestashop/api/product_features/7">
            <id>7</id>
            <custom>0</custom>
            <id_feature_value xlink:href="http://localhost/prestashop/api/product_feature_values/17">17</id_feature_value>
          </product_features>
        </product_features>
        <tags node_type="tags" />
        <stock_availables node_type="stock_availables">
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/1">
            <id>1</id>
            <id_product_attribute>0</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/8">
            <id>8</id>
            <id_product_attribute>1</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/9">
            <id>9</id>
            <id_product_attribute>2</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/10">
            <id>10</id>
            <id_product_attribute>3</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/11">
            <id>11</id>
            <id_product_attribute>4</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/12">
            <id>12</id>
            <id_product_attribute>5</id_product_attribute>
          </stock_availables>
          <stock_availables xlink:href="http://localhost/prestashop/api/stock_availables/13">
            <id>13</id>
            <id_product_attribute>6</id_product_attribute>
          </stock_availables>
        </stock_availables>
        <accessories node_type="products" />
        <product_bundle node_type="products" />
      </associations>
    </product>
</prestashop>

And the visual basic function that send the request to the webservice is the following:

Public Function executeUpdate(ByVal resource As String, ByVal idResource As String) As XmlDocument
        Dim response As String = Nothing

        Try
            Dim requestUrl As String = "http://localhost/prestashop/api/products/1"
           
            ' Serialize the updated xml file to insert a new resource and send in PUT request
            ' Read the xml modified file
            Dim xmlFile As String = My.Computer.FileSystem.ReadAllText("D:\resourceModified.xml", System.Text.Encoding.UTF8)
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(xmlFile)

            Dim webRequest2 As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(requestUrl), HttpWebRequest)
            webRequest2.Method = "PUT"
            webRequest2.ContentType = " application/x-www-form-urlencoded"
            webRequest2.Credentials = New NetworkCredential(Me.LoginName, WebServicePrestashop._password)
            webRequest2.ContentLength = byteArray.Length

            ' Get the request stream
            Using dataStream As Stream = webRequest2.GetRequestStream()
                dataStream.Write(byteArray, 0, byteArray.Length)
            End Using

            ' Get the response
            Using webResponse2 As HttpWebResponse = DirectCast(webRequest2.GetResponse(), HttpWebResponse)
                If webRequest2.HaveResponse Then
                    Using reader As New StreamReader(webResponse2.GetResponseStream(), Encoding.UTF8)
                        MsgBox(reader.ReadToEnd())
                        response = reader.ReadToEnd()
                    End Using
                Else
                    ' ...
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message.ToString())
        End Try

        If response IsNot Nothing Then
            ' ...
        Else
            Return Nothing
        End If
    End Function

Any solution?.

Thank you.

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