Step 6: Attach a policy

This page applies to Apigee and Apigee hybrid.

View Apigee Edge documentation.

Apigee policies can transform message formats, enforce access control, call remote services, authorize users, examine message content for potential threats, and do much more. For more information, see What's a policy?

Proxy and target endpoints define a pipeline for request and response message processing referred to as request and response flows. Flows are sequential stages along the API request processing path. Policies are attached at different points in the request and response message flows as processing steps.

In the following steps, you create a new policy and attach it to your API proxy in the PreFlow response flow to ensure the policy is applied before any other processing takes place. The XMLtoJSON policy used in this tutorial converts the payload of the API's response message from XML to JSON.

  1. Create an XMLtoJSON policy.
    1. Expand the helloworld API proxy bundle, position your cursor over the policies folder, and click Create icon for new policy.

      default.xml target endpoint file

      The Create policy wizard opens.

    2. Select Mediation as the policy category.
    3. Select XML to JSON as the policy type.
    4. Enter XMLtoJSON as the policy name and click Enter.

      The policy is added to the /policies folder for the API proxy and opened in the editor.

    5. Click XMLtoJSON.xml to edit the policy. Set the <OutputVariable> and <Source> elements to response and save your changes.
      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <XMLToJSON async="false" continueOnError="false" enabled="true" name="XMLtoJSON">
          <DisplayName>XMLtoJSON</DisplayName>
          <Properties/>
          <Format>yahoo</Format>
          <OutputVariable>response</OutputVariable>
          <Source>response</Source>
      </XMLToJSON>
  2. Attach the policy to the default proxy endpoint.
    1. Expand the proxies folder in the helloworld API proxy bundle and click default.xml to open the file in the editor.
    2. Add a <Step> element that references the XMLtoJSON policy to the <Response> flow in the API proxy <PreFlow>:
      <ProxyEndpoint name="default">
         <PreFlow name="PreFlow">
            <Request>
               <Step>
                  <Name>verify-api-key</Name>
               </Step>
               <Step>
                  <Name>remove-query-param-apikey</Name>
               </Step>
               <Step>
                  <Name>impose-quota</Name>
               </Step>
            </Request>
            <Response>
               <Step>
                  <Name>XMLtoJSON</Name>
               </Step>
            </Response>
         </PreFlow>
       ...
      </ProxyEndpoint>
    3. Save your changes to the file.
  3. To redeploy the environment, position your cursor over the dev environment folder and click Deploy icon for an environment.

    Deploy icon displays when you position the cursor over dev environment folder

  4. When prompted to select a test bundle to export, click Deploy without a test bundle because the test bundle has already been exported.

    Prompt to export test bundle

  5. Call your API in the Terminal tab.
    curl 0:8998/helloworld?apikey=ZQA5euYtNeJ7ZCGCJMpvd6F2BZOmxOzY

    The XML response is converted to JSON, as follows:

    {
      "root": {
        "city": "San Jose",
        "firstName": "John",
        "lastName": "Doe",
        "state": "CA"
      }
    }

Congratulations! You've successfully attached the XMLtoJSON policy to your API proxy!

Next, you will deploy your API as an archive to an Apigee environment for integration testing and production release.

1 2 3 4 5 6 (NEXT) Step 7: Deploy to an Apigee environment 8