0

I follow this Magento guide to work with Magento API REST (New Learner): http://devdocs.magento.com/guides/v2.2/get-started/order-tutorial/order-create-customer.html

In this step I try run this curl line to create a new customer:

curl -X Post "http://domain/api/rest/default/V1/customers"\
    -H "Content-Type:application/json"\
    -d '{"username":"example", "password":"example"}'

and get "curl: (6) Couldn't resolve host".

Also I want to ask, can I call this request with Postman?

5

1 Answer 1

0

You can try this command:

token=$(curl -X Post "http://domain/rest/V1/integration/admin/token"\
-H "Content-Type:application/json"\
-d '{"username":"<admin_user>", "password":"<admin_password>"}')\;
curl -X POST "http://domain/rest/default/V1/customers"\
-H "Content-Type:application/json"\
-H "Authorization:Bearer "$token\
-d '{"customer":{"email":"[email protected]", "firstname": "Example", "lastname": "Example", "id_card": "123456", "tos": "Yes"},"password":"12345678"}'

Explain: according to the official document you provided,

token=$(curl -X Post "http://domain/rest/V1/integration/admin/token"\
-H "Content-Type:application/json"\
-d '{"username":"<admin_user>", "password":"<admin_password>"}');

is getting admin token, and then store the result to a variable called $token. And then

curl -X POST "http://domain/rest/default/V1/customers"\
-H "Content-Type:application/json"\
-H "Authorization:Bearer "$token\
-d '{"customer":{"email":"[email protected]", "firstname": "Example", "lastname": "Example"},"password":"12345678"}'

is creating customer part.

I've tested this on M2.1.7, but should be more or less the same on M2.2.

8
  • I understand now this idea but still curl: (6) Couldn't resolve host > I test it with magento 2.2 . Thanks
    – Magento
    Commented Jan 2, 2018 at 7:15
  • It means your domain has problems, not the command itself.
    – PY Yick
    Commented Jan 2, 2018 at 7:26
  • token=$(curl -X Post "mysubdomain / <subdomain folder here> /rest/V1/integration/admin/token"\
    – Magento
    Commented Jan 2, 2018 at 7:46
  • Most probably it's due to wrong settings on .htaccess file. You can uncomment line 145 on this file and change to RewriteBase /<subdomain folder>/. The line number may varies on each version but it won't be far away.
    – PY Yick
    Commented Jan 2, 2018 at 7:55
  • Or method 2: You can try "mysubdomain/<subdomain folder here>/index.php/rest/V1/integration/admin/token" first. If OK then go for modify .htaccess method.
    – PY Yick
    Commented Jan 2, 2018 at 7:57

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.