2

I would like to return json responseobject via a status codes and messages for the creating customer account using magento2 REST API.

I found some documentations below:-

1) https://restfulapi.net/http-status-codes/

2) http://devdocs.magento.com/guides/v2.1/get-started/gs-web-api-response.html

API Url:- rest/V1/customer/signup

Method:- POST

Screenshot:- http://prntscr.com/ho6moe

Model code:-

<?php


namespace Test\Custom\Model;
class CustomerSignupAccount implements \Test\Custom\Api\CustomerSignupInterface
{

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Customer\Model\CustomerFactory
     */
    protected $customerFactory;


     /**
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Customer\Model\CustomerFactory    $customerFactory
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\CustomerFactory $customerFactory
    ) {
        $this->storeManager     = $storeManager;
        $this->customerFactory  = $customerFactory;

    }


    /**
     * {@inheritdoc}
    */ 

     public function customerSignup($customerData)
     {  
        // Perparing Customer Data
        $firstName      = $customerData['firstname'];       
        $lastName       = $customerData['lastname'];
        $email          = $customerData['email'];


        // Validate Customer EmailAddress
        if(!\Zend_Validate::is(trim($customerData['email']), 'NotEmpty')) 
        {                              
            return 'Please check your email, it should not empty';
        }
        if(!\Zend_Validate::is(trim($customerData['email']), 'EmailAddress'))
        {
            return 'Please check your email formate and try again';
        }           

        try{

            // Get Website ID
            $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();            
            $customerData = $this->customerFactory->create(); 
            $customerData->setWebsiteId($websiteId);
            $customerData->loadByEmail($email);
            if($customerData->getId())
            {
                return 'Customer account has been already registered with this email '.$email ;

            }else
            {
                //Instantiate object (this is the most important part)
                $customer   = $this->customerFactory->create();
                $customer->setWebsiteId($websiteId);
                // Preparing data for new customer
                $customer->setEmail($email); 
                $customer->setFirstname($firstName);
                $customer->setLastname($lastName);                      
                // Save customer data
                $customer->save();

                return 'Your account has been created successfully.';
            }


        }
        catch(\Exception $e){

            return 'Something bad happend please try again';
        }

    }

}

Note:- Here i am able to create an account it's working fine.

Please suggest me how to show response in the form of status codes and messages?

thanks

closed as unclear what you're asking by 7ochem, Manoj Deswal, Jai, Priyank, sv3n May 4 '18 at 11:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.