Search code examples
xmlweb-servicesprestashopprestashop-1.6prestashop-1.7

Prestashop api update resource error: Id is required when modifying a resource


Background:

I am attempting to update the shipping_number of a an order resource, however, all combinations I have tried seem to be failing.

Currently using the Prestashop 1.7.8.7 version (the latest as of writing this post).

I am trying to update the resource via the rest api that is available after enabling the WebService feature in my PrestaShop store. All of the fetch requests work without any problem...

Initially, the api calls were hooked up to use JSON format instead of the default XML format. However, after learning that the PUT requests only accept XML inputs, I updated this but I'm still getting an unsuccessful response.

Currently trying to get this work in Postman before I attempt to replicate in the codebase .

Steps to reproduce in Postman:

As per the Prestashop docs for updating a resource: https://devdocs.prestashop-project.org/1.7/webservice/getting-started/#update-a-resource

  1. Fetch order by id: [GET:] /api/orders/1?display=full
  2. Copy response from the fetch
  3. Paste response into request body
  4. Change value of the shipping_method node to something new/different
  5. Update order: [PUT:] /api/orders/1

enter image description here

I have also enabled more detailed error response messages by adding ini_set('display_errors', 'on'); into the config/config.inc.php file. This is show in the additional message in the response below.

Update response:

Notice:  Undefined offset: 1 in /webservice/dispatcher.php on line 40

{
    "errors": [
        {
            "code": 90,
            "message": "id is required when modifying a resource"
        }
    ]
}

Other things attempted:

Some of these attempts result in slightly different error messages (500 Internal Server Error etc.), but still fail nonetheless!

  1. Changing the http method to a POST or PATCH
  2. Updating the resource endpoint to point to /orders instead of /orders/1 (the specific order I'm trying to update)
  3. Changing the content type to text instead of xml
  4. Various different xml strucutres in the request body (see below)
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <orders>
        <order>
            ...
        </order>
    </orders>
</prestashop>
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
    <order>
        ...
    </order>
</prestashop>
<?xml version="1.0" encoding="UTF-8"?>
<order>
    ...
</order>

The only examples I can find online all look to be using the dedicated Prestashop WebServivce library which provides them with a dedicated client and allows them to call an edit() function, so haven't been of much help! Note: Using this library is also not an option on the basis my app is written in c#.


Solution

  • Ensure to POST the whole XML in a single line.

    I remember having a similar problem due unrecognized / wrong carriage returns in request.