Magento2 REST API – Fix PUT Customer Fails to Complete

bugcustomermagento2restsave

How do you PUT (update) a customer using the rest api?

I have a ticket open on github, but I was wondering if I'm just doing this wrong..

https://github.com/magento/magento2/issues/5115

I've created a customer in admin. I've noted the entity id. It is 22.

I'm attempting to PUT using the rest api to myhost.com/rest/V1/customers/{{myEntityId}}

The actual URL is http://192.168.5.65/rest/V1/customers/22 just to avoid confusion.

I'm sending the following json:

{
    "customer": {
        "firstname": "NewMitchell",
        "lastname": "NewThompson"
    }
}

Magento documentation states you only need to send the properties you want to update.

I'm expecting the customer object to be updated, but its actually trying to create a new customer. I'm seeing this message come back:

{
  "message": "Please enter a customer email.",
  "trace": "#0 /var/www/vendor/magento/module-eav/Model/Entity/VersionControl/AbstractEntity.php(90): Magento\\Customer\\Model\\ResourceModel\\Customer->_beforeSave(Object(Magento\\Customer\\Model\\Customer))\n#1 /var/www/vendor/magento/framework/Interception/Interceptor.php(74): Magento\\Eav\\Model\\Entity\\VersionControl\\AbstractEntity->save(Object(Magento\\Customer\\Model\\Customer))...blahblahblah

I'm afraid I may be doing it wrong, but at the same time, I think I may have identified a bug.

Can anyone test this and see if it works or not? I'm using Magento CE RC3, but i've tested it on Magento CE 2.0.6 and it doesn't work there either.

Best Answer

if you look at the documentation you'll find that actually the email is not optional so is needed in order to update the customer

customer-data-customer-interface {
id (integer, optional): Customer id ,
groupId (integer, optional): Group id ,
defaultBilling (string, optional): Default billing address id ,
defaultShipping (string, optional): Default shipping address id ,
confirmation (string, optional): Confirmation ,
createdAt (string, optional): Created at time ,
updatedAt (string, optional): Updated at time ,
createdIn (string, optional): Created in area ,
dob (string, optional): Date of birth ,
email (string): Email address ,
firstname (string): First name ,
lastname (string): Last name ,
middlename (string, optional): Middle name ,
prefix (string, optional): Prefix ,
suffix (string, optional): Suffix ,
gender (integer, optional): Gender ,
storeId (integer, optional): Store id ,
taxvat (string, optional): Tax Vat ,
websiteId (integer, optional): Website id ,
addresses (Array[customer-data-address-interface], optional): Customer addresses. ,
disableAutoGroupChange (integer, optional): Disable auto group change flag. ,
extensionAttributes (customer-data-customer-extension-interface, optional),
customAttributes (Array[framework-attribute-interface], optional): Custom attributes values.
}

but if you try with the email at the current release you'll see there also another param that is not optional (problably the documentation has to be updated). here is the very least needed to do the update:

{
  "customer": {
  "email": "email@here.me",
  "firstname": "firstname new",
  "lastname": "lastname new",
  "website_id": 1
  }
}