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
  • Yes you can use postman for this call you need to authorize the connection Commented Jan 1, 2018 at 18:18
  • authorize the connection via token ? Commented Jan 1, 2018 at 19:27
  • any guide i New Learner Commented Jan 1, 2018 at 19:28
  • Yes authorize the connection using token Commented Jan 2, 2018 at 4:00
  • You can also view this solution also here magento.stackexchange.com/questions/170921/… Commented Jan 2, 2018 at 7:12

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 Commented Jan 2, 2018 at 7:15
  • It means your domain has problems, not the command itself. Commented Jan 2, 2018 at 7:26
  • token=$(curl -X Post "mysubdomain / <subdomain folder here> /rest/V1/integration/admin/token"\ 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. 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. 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.