Managing REST API Requests and Responses Using Postman
Postman is a Chrome application that can send requests to a RESTFul web service. It can be used to test the APIs and capture HTTP requests and responses.
Initial Postman Setup
Before you start managing requests, use your Google Chrome browser to download Postman from https://www.getpostman.com/apps.
Retrieving an Access Token and Authorizing APIs in Postman
Before you send requests using Postman, you must specify the necessary credentials to access the APIs, including an access token.
To retrieve an access token, you must first obtain credentials and create a Base64 encoded key. The steps to complete that process is described in Using OAuth 2.0 Authentication.
To retrieve an access token in Postman:
- Launch Postman.
- In the Request Builder, select POST as the request type.
- Include the URL to the token endpoint. Example:
https://cxapi-stage.opower.com/apis/authorization-v1/oauth2/token
- Select the Headers tab. The header keys and values are displayed.
- Define the header as follows:
-
Key:
Authorization
-
Value:
WhereBasic EncodedKey
EncodedKey
is the Base64 encoded key created when obtaining client credentials. - Description: Optional, can be left blank.
-
- Select the Body tab. The body options are displayed.
- Define the body as follows:
-
Content Type:
x-www-form-urlencoded
-
Key:
grant_type
-
Value:
client_credentials
- Description: Optional, can be left blank.
-
- Click Send. A response is displayed.
-
Verify that the response is correct. The
client_id
value must match theclient_id
provided by Oracle Utilities Opower. -
Select the Headers, tab and then from the Presets drop-down list, select Manage Presets. The Manage Header Presets dialog box opens.
- Click Add.
- Provide a name and description for the new header preset, and then click Add.
-
Click Save to save the request.
Refreshing an Access Token
If you have already retrieved an access token and saved the response as described in the process above, you can refresh the access token when it expires. Tokens expire after one hour.
-
Launch Postman.
-
Open the saved request to create an access token.
-
Click Send. A response is displayed.
-
Select the Headers tab, and then from the Presets drop-down list, select Manage Presets. The Manage Header Presets dialog box opens.
- Select the preset header and update it with the refreshed access token, and then click Update.
- Click Save to save changes to the request.
Example: Retrieving a Customer Bill Comparison
This example provides detailed instructions on how to retrieve a customer bill comparison using Postman. This also serves as an example of using multiple requests. Resources are queried by their unique identifier. In many cases, you must combine multiple calls to retrieve the desired information when starting from information other than the identifier.
To retrieve a customer bill with Postman:
- Launch Postman.
- In the Request Builder, specify the URL of the resource that you want to access. In this example, the URL to access the resource is
https://cxapi-stage.opower.com/apis/customer-data-v2/utilitycode/customers?8207421000
with the utility account number of8207421000
. Replaceutilitycode
with the applicable utility code. - In the drop-down next to the request URL, select GET, and then click Send. The following response is returned:
The response above includes the Oracle Utilities Opower customer ID of{ response": { "name":"John Doe", "id":686380, "recipient":"RECIPIENT" } }
686380
, which you can use to retrieve the associated utility account IDs. - In the Request Builder, specify the URL to access the resource as
https://cxapi-stage.opower.com/apis/customer-data-v2/utilitycode/customers/686380/utility_accounts
. Replaceutilitycode
with the applicable utility code. - In the drop-down next to the request URL, select GET, and then click Send. The following response is returned:
{ response": [ "id":807503, "meterType":"ELEC", "utilityAccountId":"8207482852", "moveInDate":"2008-04-04", "resolution":"BILLING", "utilityAccountId2":"8207482852" }, { "id":1566055, "meterType":"GAS", "utilityAccountId":"8207482851", "moveInDate":"2008-04-04", "resolution":"BILLING", "utilityAccountId2":"8207482851" }] }
The response above includes the two associated utility account IDs of
807503
and1566055
. You can use the customer's gas account ID to retrieve the associated bill comparison. - In the Request Builder, specify the URL to access the resource as
https://cxapi-stage.opower.com/apis/billComparison-v2/utilitycode/bill-comparison/accounts/1566055
. Replaceutilitycode
with the applicable utility code. - In the drop-down next to the request URL, select GET, and then click Send. The following response is returned:
{ response": { "currencySymbol":"$", "reference": { "usage":18.0, "averageTemperature":57.551723, "startDate":"2011-04-01", "endDate":"2011-04-29", "charges":28.55 }, { "temperatureUnit":"FAHRENHEIT", "compared": { "usage":36.0, "averageTemperature":44.258064, "startDate":"2011-03-01", "endDate":"2011-03-31", "charges":43.83 }, "meterUnit":"THERM", "analysisResults": [ { "analysisName":"WEATHER", "costDifferenceExplained":0.0 }, { "analysisName":"NUM_DAYS", "costDifferenceExplained":-2.8277419 }, { "analysisName":"OTHER", "costDifferenceExplained":-12.45 }] { }