Skip to content

Latest commit

 

History

History
162 lines (144 loc) · 10.5 KB

gs-rest-ff-rest-client.md

File metadata and controls

162 lines (144 loc) · 10.5 KB
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.

Step 1: Get the Firefox REST Client add-on

If you haven't already done so, install the Firefox REST Client.

Step 2: Look up the call in webapi.xml

This section discusses how to make an HTTP POST call to the createCustomer() method.

  1. Open webapi.xml.
  2. 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.

Step 3: Find the service data object

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:

<script src="https://gist.github.com/xcomSteveJohnson/9775420.js"></script>

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.

Step 4: Find getters on customer

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:

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 becomes customer
  • getAddresses becomes addresses

Step 5: Find getters on Address and Customer

\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).

Step 6: Construct the JSON object

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.

Step 7: Create and activate the integration

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:

  1. Log in to the Magento Admin as an administrator.
  2. Click System > Integrations.
  3. In the upper right corner, click Add New Integration.
  4. 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.
  5. In the left navigation bar, click API to assign resources to integration.
  6. Select the Customers check box.
  7. Optionally select other check boxes to allow the integration access to those resources.
  8. Click Save.
    The integration displays on the Integrations page.
  9. Click Activate next to the name of the integration you just created.
    The following figure shows an example.
    Click an integration's activate text to allow it to access defined resources
  10. 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.

Step 8: Get started with the Firefox REST Client

Now you can start building the REST call using the Firefox REST Client as follows:

  1. Start the Firefox web browser and the Firefox REST Client.
  2. From the Method list, click POST.
  3. 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.

Step 9: Set up authorization for your REST call

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:

  1. Click Authentication > OAuth.
  2. Enter your OAuth credentials.
    The following figure shows an example.
  3. Optionally select the Remember the setting check box to avoid having to enter the credentials in future REST calls.
  4. Click Insert > Insert as header.
  5. Click Yes, please at the confirmation dialog box.

Step 10: Complete the REST call

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:

  1. Click Headers > Custom Header.
  2. In the Name field, enter Content-Type.
  3. In the Value field, enter application/json.
  4. Click Okay.
    The Firefox REST Client should now look like the following:
  5. 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>
  6. 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>

Related topics