Question

I have created a custom customer attribute(mobile_number) and set default value of customer create account.

I added mobile number to customer_attribute:

    $customerSetup->addAttribute(Customer::ENTITY, 'mobile_number', [
        'type' => 'varchar',
        'label' => 'Mobile Number',
        'input' => 'text',
        'required' => true,
        'visible' => true,
        'user_defined' => true,
        'sort_order' => 1000,
        'position' => 1000,
        'system' => 0,
    ]);

Now, its added, when created account manually its working fine and this mobile number details stored in customer_entity_varchar table.

I need to create a customer using rest api. I followed this link to send request via curl it throws an exeption that Mobile Number is required.

Curl Request:

curl -X POST "http://www.magento_server.net/index.php/rest/V1/customers" -H "Content-Type:application/json" -H "Authorization: Bearer xhfyo8cfvknpyd5dwuupe34uosh6tuhj" -d '{
    "customer": {
      "email": "person@email.net",
      "firstname": "First",
      "lastname": "Last",
      "storeId": 1,
      "websiteId": 1

    },
     "password": "Abc@123456",
     "mobile_number": "123-123-1234"
  }' | json_pp

Error:

{
   "trace" : "#0 /var/www/html/vendor/magento/module-customer/Model/ResourceModel/Customer.php(143): Magento\\Customer\\Model\\ResourceModel\\Customer->_validate(Object(Magento\\Customer\\Model\\Customer))\n#1 /var/www/html/vendor/magento/module-eav/Model/Entity/VersionControl/AbstractEntity.php(90): Magento\\Customer\\Model\\ResourceModel\\Customer->_beforeSave(Object(Magento\\Customer\\Model\\Customer))\n#2 /var/www/html/vendor/magento/framework/Model/AbstractModel.php(615): Magento\\Eav\\Model\\Entity\\VersionControl\\AbstractEntity->save(Object(Magento\\Customer\\Model\\Customer))\n#3 /var/www/html/vendor/magento/module-customer/Model/ResourceModel/CustomerRepository.php(195): Magento\\Framework\\Model\\AbstractModel->save()\n#4 [internal function]: Magento\\Customer\\Model\\ResourceModel\\CustomerRepository->save(Object(Magento\\Customer\\Model\\Data\\Customer), 'd9f8c7b5aed6d1f...')\n#5 /var/www/html/vendor/magento/framework/Interception/Interceptor.php(144): call_user_func_array(Array, Array)\n#6 /var/www/html/var/generation/Magento/Customer/Model/ResourceModel/CustomerRepository/Interceptor.php(27): Magento\\Customer\\Model\\ResourceModel\\CustomerRepository\\Interceptor->___callPlugins('save', Array, Array)\n#7 /var/www/html/vendor/magento/module-customer/Model/AccountManagement.php(527): Magento\\Customer\\Model\\ResourceModel\\CustomerRepository\\Interceptor->save(Object(Magento\\Customer\\Model\\Data\\Customer), 'd9f8c7b5aed6d1f...')\n#8 /var/www/html/vendor/magento/module-customer/Model/AccountManagement.php(484): Magento\\Customer\\Model\\AccountManagement->createAccountWithPasswordHash(Object(Magento\\Customer\\Model\\Data\\Customer), 'd9f8c7b5aed6d1f...', '')\n#9 [internal function]: Magento\\Customer\\Model\\AccountManagement->createAccount(Object(Magento\\Customer\\Model\\Data\\Customer), 'Abc@123456', '')\n#10 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(265): call_user_func_array(Array, Array)\n#11 /var/www/html/vendor/magento/module-webapi/Controller/Rest.php(160): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#12 /var/www/html/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(24): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#13 /var/www/html/vendor/magento/framework/App/Http.php(115): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#14 /var/www/html/vendor/magento/framework/App/Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#15 /var/www/html/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#16 {main}",
   "message" : "\"Mobile Number\" is a required value."
}

Suggest me How to add our custom customer attribute field to rest api curl request and what miss in this query.

Was it helpful?

Solution

The custom attribute mobile_number should be inside custom customer attribute data:

{
  "customer": {
    "email": "person@email.net",
    "firstname": "First",
    "lastname": "Last",
    "storeId": 1,
    "websiteId": 1,
    "custom_attributes": [
     {
       "attribute_code": "mobile_number",
       "value": "23456798"
     }
    ]
  },
  "password": "Abc@123456"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top