layout | group | subgroup | title | menu_title | menu_order | version | github_link | redirect_from |
---|---|---|---|---|---|---|---|---|
default |
Step 3. Use cURL to run the request |
Firefox REST client |
Firefox REST client |
3 |
2.0 |
get-started/gs-rest-ff-client.md |
/guides/v1.0/get-started/gs-rest-ff-client.html |
This topic discusses how to use the Firefox REST Client browser add-on to send a REST call that creates a new Magento customer.
To do this, you need to set up an integration that provides access to Magento customer resources. You set up the integration using the Magento Admin.
You don't need to do any coding in this example.
If you haven't already done so, install the Firefox REST Client.
This section discusses how to make an HTTP POST call to the createCustomer()
method.
- Open webapi.xml.
Find the desired call; for example,
<route url="/V1/customerAccounts" method="POST"> <service class="Magento\Customer\Service\V1\CustomerAccountServiceInterface" method="createCustomer"/> <resources> <resource ref="anonymous"/> </resources> </route>
The route url
specifies the URI of the REST call. (Step 10 shows the entire URL.)
In this example, the URI is POST /V1/customerAccounts
- Any value prefixed by a colon character is a required input.
- Some REST calls have no route; for these cases, use the Base URL only.
The service data object tells you what data to pass in to the REST API. The service data object is specified by the service interface method named by service class
in webapi.xml
.
Continuing the preceding example, the createCustomer()
method on the \Magento\Customer\Service\V1\CustomerAccountServiceInterface
specifies the data service object as follows:
In this case, the service object is \Magento\Customer\Service\V1\Data\customer.
To use customer
as a JSON or XML parameter in the POST call payload, you must specify it as follows: customer_details
. The parameter name is all lowercase with camel case strings separated by an underscore character. To use it as JSON input, customer_details
must specify a JSON object.
Getters on service data objects enable you to find what data is required to execute the action (in this case, create a customer).
There are two getters on customer
:
getAddresses()
, which returns data defined by the service data object \Magento\Customer\Service\V1\Data\AddressNote that the
@return
specifies\Magento\Customer\Service\V1\Data\Address[]|null
, which means that null values are accepted (in other words, you don't have to pass any data in).getCustomer()
, which returns data defined by \Magento\Customer\Service\V1\Data\Customer.
To use getAddresses
and getCustomer
as JSON or XML values, remove get
and convert the remainder of the string to lowercase separated by underscores. In this case,
getCustomer
becomescustomer
getAddresses
becomesaddresses
\Magento\Customer\Service\V1\Data\Address and \Magento\Customer\Service\V1\Data\Customer have several getters, all of which are optional. Each getter has @return
that tells you the data type.
Pick a few values to create your customer record; remember to use the same rules in step 3 (that is, remove get
, convert everything to lowercase, and separate camel case letters with underscores).
Now that you know all the data you need to create a customer, you can create the JSON necessary to create the customer. A sample follows:
<script src="https://gist.github.com/xcomSteveJohnson/3901c6cf9d41964bd319.js"></script>You'll use this in step 10.
For OAuth 1.0a authorization to work, you must create an integration that optionally has access to customer resources. (Although the createCustomer
method can authenticate anonymously, it's good practice to grant access to customer objects anyway.)
The integration also provides the following OAuth 1.0a authorization details:
- Consumer key
- Consumer secret
- Access token
- Access token secret
To create the integration:
- Log in to the Magento Admin as an administrator.
- Click System > Integrations.
- In the upper right corner, click Add New Integration.
- On the New Integration page, enter a unique name for the integration in the Name field.
You can leave all other fields on this page blank. - In the left navigation bar, click API to assign resources to integration.
- Select the Customers check box.
- Optionally select other check boxes to allow the integration access to those resources.
- Click Save.
The integration displays on the Integrations page. - Click Activate next to the name of the integration you just created.
The following figure shows an example.
- At the confirmation dialog box, click Allow.
The Integration Tokens for Extensions dialog box displays the authorization credentials you'll need for the REST call. You can view these credentials at any time by logging in to the Magento Admin and editing the integration.
Now you can start building the REST call using the Firefox REST Client as follows:
- Start the Firefox web browser and the Firefox REST Client.
- From the Method list, click POST.
- In the URL field, enter the following:
https:////rest/default/V1/customerAccounts
The following figure shows an example.
Remember that /V1/customerAccounts
is the REST route from step 2.
Before you continue, make sure you have the following for the integration you created in step 8:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
In the Firefox REST Client:
Now that you have your Authorization
header set, you must set one more headers for the Magento system to accept the REST call: Content-Type: application/json
. Finally, add the request body and send the request.
To complete your REST call:
- Click Headers > Custom Header.
- In the Name field, enter
Content-Type
. - In the Value field, enter
application/json
. - Click Okay.
The Firefox REST Client should now look like the following:
- In the Body field, enter your JSON data to create the customer.
An example follows:
<script src="https://gist.github.com/xcomSteveJohnson/3901c6cf9d41964bd319.js"></script> - Click Send.
If the REST call succeeded, the Response Headers tab page at the bottom should show 200 OK
on the first line.
If the REST call failed, click the Response Body (Raw) tab page to display the XML data you get back from Magento.
Following is sample raw data from a successful REST call:
<script src="https://gist.github.com/xcomSteveJohnson/fed484b41f9fbdd46331.js"></script>