Send an HTTP request

APPLIES TO: Composer v1.x and v2.x

This article will teach you how to send an HTTP request using OAuth for authorization. It's not necessary to deploy your bot to Azure for this to work.

Prerequisites

Set up OAuth in Composer

  1. In Composer, create a bot using the Empty bot template. Let's call it testhttp.

  2. Optionally, in the Greetings trigger, set the message to Testing HTTP request.

  3. Follow the steps described in the article Add user authentication to set up OAuth in your bot to access GitHub. The Unknown intent trigger is modified as shown in the figure below.

    OAuth response to test

The Send a response message contains ${dialog.oauth.token} that is the token returned by GitHub after the connection with the bot has been succeeded.

  1. Select Start Bot, then Test in Emulator.

  2. In the emulator, enter a short message. The bot will display a Login card.

  3. Select Login and follow the required steps. GitHub will send back the connection token.

    OAuth test token

Now, with the OAuth setup ready and token successfully obtained, you are ready to add the HTTP request in your bot.

Add a Send an HTTP request action

  1. In Composer, select Design from the menu on the left.

  2. Select the Unknown intent trigger.

  3. In the canvas, select the + icon, under Send a response.

  4. From the drop-down menu select Access external resources and then Send an HTTP request.

    HTTP action]

  5. In the Properties panel, set the method to GET and set the URL to your target API. For example, a typical GitHub API URL such as https://api.github.com/users/your-username/orgs.

  6. Add headers to include more info in the request. For example we can add two headers to pass in the authentication values in this request.

    1. In the first line of header, add Authorization in the Key field and bearer ${dialog.token.token} in the Value field. Press Enter.
    2. In the second line of header, add User-Agent in the Key field and Vary in the Value field. Press Enter.
  7. Finally, set the Result property to dialog.api_response and Response type to json.

    Note

    The HTTP action sets the following information in the Result property: statusCode, reasonPhrase, content, and headers. Setting the Result property to dialog.api_response means you can access those values via dialog.api_response.statusCode, dialog.api_response.reasonPhrase, dialog.api_response.content and dialog.api_response.headers. If the response is json, it will be a deserialized object available via dialog.api_response.content.

Test

Add an If/else branch to test the response of this HTTP request.

  1. In the canvas, select the + icon, under Send an HTTP request.

  2. From the drop-down menu select Create a condition and then Branch:If/else.

    HTTP test

  3. In the Properties panel, set Condition to dialog.api_response.statusCode == 200.

  4. Add two Send a response actions to be fired based on the testing results (true/false) of the condition. This means if dialog.api_response.statusCode == 200 is evaluated to be true, send a response called with success! ${dialog.api_response}, else send a response calling api failed.

    HTTP test results

  5. Restart your bot and test it in the Emulator. After logging in successfully, you should be able to see the response content of the HTTP request.