1

I am trying to create customer using API in magento 2.2.3.

Below api is working well and customer can created successfully but issue only arrive while adding address.

If i send address field with customer magento show me error like below.

Here is the data i am sending to api

Array
(
    [customer] => Array
        (
            [website_id] => 1
            [store_id] => 1
            [group_id] => 1
            [firstname] => name
            [lastname] => lastname
            [custom_attributes] => Array
                (
                    [customer_code] => 3654256542156954
                )

            [email] => example@gmail.com
            [addresses] => Array
                (
                    [region] => 67
                    [country_id] => CA
                    [countryId] => CA
                    [street] => 3652 Rohnstand Road  
                    [firstname] => name
                    [lastname] => lastname
                    [company] => 3654256542156954
                    [telephone] => 999-999-9999
                    [city] => city
                    [postcode] => pincode
                )

        )

    [password] => password
)

This is how api calling.

    $data = $this->mapData($data);
    $ch = curl_init("http://127.0.0.1/project/index.php/rest/V1/customers");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($this->_token)));

    $result = curl_exec($ch);

Response i got from API

Array
(
    [message] => One or more input exceptions have occurred.
    [errors] => Array
        (
            [0] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => firstname
                        )

                )

            [1] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => lastname
                        )

                )

            [2] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => street
                        )

                )

            [3] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => city
                        )

                )

            [4] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => telephone
                        )

                )

            [5] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => postcode
                        )

                )

            [6] => Array
                (
                    [message] => %fieldName is a required field.
                    [parameters] => Array
                        (
                            [fieldName] => countryId
                        )

                )

)

[trace] => #0 /var/www/html/project/vendor/magento/module-customer/Model/AccountManagement.php(789): Magento\Customer\Model\ResourceModel\AddressRepository->save(Object(Magento\Customer\Model\Data\Address))
#1 /var/www/html/project/generated/code/Magento/Customer/Model/AccountManagement/Interceptor.php(141): Magento\Customer\Model\AccountManagement->createAccountWithPasswordHash(Object(Magento\Customer\Model\Data\Customer), 'b54a5a70013dd9b...', '')
#2 /var/www/html/project/vendor/magento/module-customer/Model/AccountManagement.php(724): Magento\Customer\Model\AccountManagement\Interceptor->createAccountWithPasswordHash(Object(Magento\Customer\Model\Data\Customer), 'b54a5a70013dd9b...', '')
#3 /var/www/html/project/generated/code/Magento/Customer/Model/AccountManagement/Interceptor.php(128): Magento\Customer\Model\AccountManagement->createAccount(Object(Magento\Customer\Model\Data\Customer), '$000002$', '')
#4 [internal function]: Magento\Customer\Model\AccountManagement\Interceptor->createAccount(Object(Magento\Customer\Model\Data\Customer), '$000002$', '')
#5 /var/www/html/project/vendor/magento/module-webapi/Controller/Rest.php(330): call_user_func_array(Array, Array)
#6 /var/www/html/project/vendor/magento/module-webapi/Controller/Rest.php(239): Magento\Webapi\Controller\Rest->processApiRequest()
#7 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#8 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)
#9 /var/www/html/project/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#10 /var/www/html/project/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(39): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#11 /var/www/html/project/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#12 /var/www/html/project/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#13 /var/www/html/project/vendor/magento/framework/App/Bootstrap.php(256): Magento\Framework\App\Http\Interceptor->launch()
#14 /var/www/html/project/index.php(40): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#15 {main}
)
  • I can guide through POSTMAN application to add customer using API, but don't have any idea for script. – Aditya Shah Jun 5 '18 at 12:10
  • you can give ans here for postman. i think issue data that i am passing not in script – Vaibhav Ahalpara Jun 5 '18 at 12:15
  • Yeah sure, I am giving answer and which is working in POSTMAN – Aditya Shah Jun 5 '18 at 12:18
  • i need to add customer and address both – Vaibhav Ahalpara Jun 5 '18 at 12:18
  • Okay, for address i need to add params – Aditya Shah Jun 5 '18 at 12:25
1

Register Customer using API

webapi.xml

<route url="/V1/customers" method="POST">
<service class="Magento\Customer\Api\AccountManagementInterface" method="createAccount"/>
<resources>
    <resource ref="anonymous"/>
</resources>
</route>

url http://<magento-host>/rest/V1/customers

url action :: POST

Body > raw > JSON(application/json)

{
"customer":{
    "email":"Test@example.com",
    "firstname":"Aditya",
    "lastname":"Shah",
    "websiteId":"1",
    "addresses":[
        {
            "customer_id":"1",
            "firstname":"Aditya",
            "lastname":"Shah",
            "company":"ABC Manufacturing",
            "telephone":"555-555-5555",
            "city":"Boston",
            "region":"Massachusetts",
            "postcode":"02115",
            "country_id":"US",
            "street":[
                "404 Main Street",
                "PO Box 321"
            ]
        }
    ]
},
 "password": "Account@123"
}
  • i want to add address while creating customer. – Vaibhav Ahalpara Jun 5 '18 at 12:27
  • My code is working while creating customer. but it give me error while i am adding address fields – Vaibhav Ahalpara Jun 5 '18 at 12:27
  • Yes, i am implementing the same bro :) – Aditya Shah Jun 5 '18 at 12:31
  • please check answer with address fields [UPDATED] – Aditya Shah Jun 5 '18 at 12:49
  • @VaibhavAhalpara Did my answer solved your concern ? – Aditya Shah Jun 6 '18 at 4:43
0

Data format was wrong while passing data to api.

we need to pass addresses as multidimensional array.

Here is a data format.

Array
(
    [customer] => Array
        (
            [website_id] => 1
            [store_id] => 1
            [group_id] => 1
            [firstname] => name
            [lastname] => lastname
            [custom_attributes] => Array
                (
                    [customer_code] => 3654256542156954
                )

            [email] => example@gmail.com
            [addresses] => Array(
                [0] => Array
                        (
                            [region] => 67
                            [country_id] => CA
                            [countryId] => CA
                            [street] => 3652 Rohnstand Road  
                            [firstname] => name
                            [lastname] => lastname
                            [company] => 3654256542156954
                            [telephone] => 999-999-9999
                            [city] => city
                            [postcode] => pincode
                        )
                )

        )

    [password] => password
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.