When using a REST API , logic may be added to customize the information that is sent in requests or received in responses. For that, you have two properties in the REST API that is consuming the web service:

After setting the value, an action with the same name is made available under the REST API.

Use it to add the logic to customize the request or response.

Example

Imagine you want to build an application in OutSystems to search for companies in LinkedIn and display the information available. LinkedIn provides a REST API with authentication for that effect.

Go to the LinkedIn API documentation and understand how their REST API works:

Implement the application as described in Get User Authorization, but with the following data:

1. Redirect to Get the User Authorization

1.1. Look Into the API Documentation

Navigate to the following URL:

https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_REDIRECT_URI&state=STATE

where:

YOUR_CLIENT_ID: this is the key assigned to the Box account of your application;

YOUR_REDIRECT_URI: this is the URL of the page in your application where users are redirected to, after authorizing your application to access their data;

STATE: a random security string you send and LinkedIn sends back to you together with the authorization token, for you to validate it is your token.

1.2. Add the Logic to Get the User Authorization

Use the following values:

response_type: set to "code";

client_id: set with the key of your application in the LinkedIn account;

redirect_uri: This is the URL of the web screen LinkedIn navigates after the user has authenticated to get the authorization. Create a web screen called 'linkedinoauthcallback' and set this input parameter to:

MakeAbsoluteURL(GetOwnerURLPath() + "linkedinoauthcallback" + If(GetApplicationServerType() = "J2EE", "jsf", "aspx");

state: set an arbitrary string, for example, GeneratePassword(20,True). Store the string in a session variable to validate later when LinkedIn navigates back to your application with the authorization token.

Implement the callback logic in the 'linkedinoauthcallback' web screen:

2. Get the Authorization Token

2.1. Look Into the API Documentation

Use the following values for the PostAccessToken REST API method:

where:

CODE: set to "code";

YOUR_CLIENT_ID: this is the key assigned to the LinkedIn account of your application;

YOUR_REDIRECT_URI: this is the URL of the page in your application where users are redirected to, after authorizing your application to access their data;

STATE: a random security string you send and LinkedIn sends back to you together with the authorization token, for you to validate it is your token.

To test request/response structures, use the console of the service provider (if any) or a generic console like apigee or cURL.

2.2. Understand What is Created

Change the name of the REST API to 'LinkedInAuthentication'.

3. Execute the Service

3.1. Look Into the API Documentation

To search for a company in LinkedIn, use the following information:

3.2. Define the REST API and REST API Method

Fill the form with the above data, and the following Method URL:

https://api.linkedin.com/v1/companies/universal-name={universal_name}:(id,name,description,website-url,logo-url,employee-count-range,locations)

Now, let's do the customization of the request.

According to the API documentation, the REST API method requires the authorization token to be sent in the URL. For that, change the request before sending it top include the authorization token in the URL.

Make the OnBeforeRequest action available by setting it in the 'On Before Request' property of the REST web service.

Design the action logic to add the authorization token to the request URL:

3.4. Use the REST API Method in your Application

Search for a company and show it on the Company web screen:

  1. Add an input for the search string;
  2. Add a search button and the Search screen action associated with the button;
  3. Add the REST API method that gets the company to the Search screen action. Set the input parameter with the search string;
  4. Use a local variable to hold the result of the REST API method;
  1. Use a form to show the company data:
    1. Set the local variable as source record;
    2. Add the fields you want to show.
  2. Use a   table records to show the list of addresses:
    1. Set the list of addresses in the local variable as source;
    2. Add the columns you want to show.

Publish and open in the browser.

See Also

Advanced Customizations