Php Magento Api Rest Create Customer Password Issue :
Asked Answered
M

1

6

I'm using the Magento ver. 2.1.2 Rest Api to create users, following this : http://devdocs.magento.com/guides/m1x/api/rest/Resources/resource_customers.html#RESTAPI-Resource-Customers-HTTPMethod-POST-customers

$data = [
        "customer" => [
            "firstname" => 'Earl',
            "lastname" => 'Hickey',
            "email" => '[email protected]',
            "password" => 'password',
            "website_id" => 1,
            'store_id' => 1,
            "group_id" => 1
        ]
    ];

    $token = $this->get('lp_api')->getToken();
    $ch = curl_init( $this->endpoint . '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_VERBOSE,  true);       
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json", "Authorization: Bearer " . json_decode( $token ),
        )
    );

        // var_dump(curl_getinfo($c));
    $result = curl_exec($ch);

If i send a password (as in the example above), i've got the following error :

Next Exception: Report ID: webapi-583357a3bf02f; Message: Property "Password" does not have corresponding setter in class "Magento\Customer\Api\Data\CustomerInterface". in /var/www/html/www.magento.dev/vendor/magento/framework/Webapi/ErrorProcessor.php:195

I noticed that if i remove the "password" => 'password' from the $data array, a user is created without password (seems odd to me).

I can't find any help on this error. Any idea anyone ?

Myra answered 21/11, 2016 at 20:33 Comment(1)
Check here step by step guide: magento.stackexchange.com/questions/150581/…Urey
I
3

Refer below link for Magento 2.x version. http://devdocs.magento.com/swagger/index_20.html#/

I have used below body for creating customers through Rest Api and it worked properly.

{ "customer": {

"email": "[email protected]", "firstname": "x", "lastname": "y", "website_id":1, "group_id":1, "custom_attributes": [ { "attribute_code": "mobile_no", "value": "1234567890" } ]

},

"password": "123456"

}

Indispose answered 22/11, 2016 at 9:31 Comment(3)
Thanks, works like a charm. But how come, that on that doc, the password is part of the customer ? devdocs.magento.com/guides/m1x/api/rest/Resources/…. Last question, on the swagger, where exactly did you find this info ? The only thing i can find is "customerAccountManagementV1" and ther is no mention of password here.Myra
Link: devdocs.magento.com/guides/m1x/api/rest/Resources/…Indispose
Sorry for the above comment. Link: devdocs.magento.com/guides/m1x/api/rest/Resources/… is used for Magento 1.x version. In the doc, they had given example only and I have shared the way I have used it. In swagger, you can click on customerCustomerRepositoryV1 link where you will get all customer related API with its example.Indispose

© 2022 - 2024 — McMap. All rights reserved.