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] => [email protected]
            [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}
)
5
  • I can guide through POSTMAN application to add customer using API, but don't have any idea for script. Commented Jun 5, 2018 at 12:10
  • you can give ans here for postman. i think issue data that i am passing not in script Commented Jun 5, 2018 at 12:15
  • Yeah sure, I am giving answer and which is working in POSTMAN Commented Jun 5, 2018 at 12:18
  • i need to add customer and address both Commented Jun 5, 2018 at 12:18
  • Okay, for address i need to add params Commented Jun 5, 2018 at 12:25

2 Answers 2

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":"[email protected]",
    "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"
}
6
  • i want to add address while creating customer. Commented Jun 5, 2018 at 12:27
  • My code is working while creating customer. but it give me error while i am adding address fields Commented Jun 5, 2018 at 12:27
  • Yes, i am implementing the same bro :) Commented Jun 5, 2018 at 12:31
  • please check answer with address fields [UPDATED] Commented Jun 5, 2018 at 12:49
  • @VaibhavAhalpara Did my answer solved your concern ? Commented Jun 6, 2018 at 4:43
1

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] => [email protected]
            [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 and acknowledge you have read our privacy policy.

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