2

I use curl to call the web api to create a customer account according to the example here.

curl -X POST "http://local.magento/index.php/rest/V1/customers" \
     -H "Content-Type:application/json" \
     -d '{
    "customers": {
        "customer": {
            "email": "user@example.com",
            "firstname": "John",
            "lastname": "Doe"
        },
        "addresses": [
            {
                "defaultShipping": true,
                "defaultBilling": true,
                "firstname": "John",
                "lastname": "Doe",
                "region": {
                    "regionCode": "CA",
                    "region": "California",
                    "regionId": 12
                },
                "postcode": "90001",
                "street": ["Zoe Ave"],
                "city": "Los Angeles",
                "telephone": "555-000-00-00",
                "countryId": "US"
            }
        ]
    }
}'

It returns error as below.

{"message":"%fieldName is a required field.","parameters":{"fieldName":"customer"}}

What's wrong with it?

7
  • These error normally occurs if it is not getting username & password as it's params. – Arjun Jul 30 '16 at 6:54
  • I added "password": "123456" under lastname but it still failed with the same error – Paul Jul 30 '16 at 7:01
  • Try this method. it works. blog.i13websolution.com/magento-2-rest-api-example – Arjun Jul 30 '16 at 7:21
  • @Arjun, it is not the same problem. – Paul Jul 30 '16 at 10:59
  • First did you get auhorized token by using est/V1/integration/customer/token then only /rest/V1/customers could be applied – Arjun Jul 30 '16 at 11:17
0

The syntax that works is as follows:

curl -X POST "http://local.magento/index.php/rest/V1/customers" \
 -H "Content-Type:application/json" \
 -d '{
       "customer": {
          "email": "user@example.com",
          "firstname": "John",
          "lastname": "Doe"
       },
       "password": "mypassword"
     }'

And no, you don't need to pass in any authentication token for this to work.

1
  • getting below error "message": "The consumer isn't authorized to access %resources.", – jafar pinjar Jan 10 '19 at 14:20
0

Format of post data should be as following:

curl -X POST "http://local.magento/index.php/rest/V1/customers" \
     -H "Content-Type:application/json" \
     -d '{
    "customer": {
        "email": "user@example.com",
        "firstname": "John",
        "lastname": "Doe",
        "addresses": [
            {
                "defaultShipping": true,
                "defaultBilling": true,
                "firstname": "John",
                "lastname": "Doe",
                "region": {
                    "regionCode": "CA",
                    "region": "California",
                    "regionId": 12
                },
                "postcode": "90001",
                "street": ["Zoe Ave"],
                "city": "Los Angeles",
                "telephone": "555-000-00-00",
                "countryId": "US"
            }
        ]
    }
}'

For further information please take a look at the file, vendor/magento/module-customer/Api/AccountManagementInterface.php

/**
 * Create customer account. Perform necessary business operations like sending email.
 *
 * @param \Magento\Customer\Api\Data\CustomerInterface $customer
 * @param string $password
 * @param string $redirectUrl
 * @return \Magento\Customer\Api\Data\CustomerInterface
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function createAccount(
    \Magento\Customer\Api\Data\CustomerInterface $customer,
    $password = null,
    $redirectUrl = ''
);

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.