Send simple HTTP requests

View as Markdown

Let us take a deep dive into the request method. Using this method, you can send HTTP requests to a web server open to internet. It is a simple but powerful way to add all kinds of external functionality to SWML scripts.

Reference available

This is a guide for the request method. The reference for this method can be found in here.

We will start by pulling a random dad joke from the internet and reading it out loud to the user.

1version: 1.0.0
2sections:
3 main:
4 - answer: {}
5 - play:
6 url: 'say:Hello! I can tell you a random dad joke. Please wait a moment.'
7 - request:
8 url: 'https://icanhazdadjoke.com/'
9 method: GET
10 headers:
11 Accept: application/json
12 save_variables: true
13 - play:
14 url: 'say:${request_response.joke}'
15 - hangup: {}

The following points should be noted:

  1. After answering the phone call and playing a short welcome message, we request a joke from the website icanhazdadjoke.com.

  2. We specifically state that we want the response to be in JSON format using the Accept header.

  3. We have set the save_variables option to be true. SWML can parse the JSON response for you and save all the response objects into a variable named request_response.

  4. The server gives back a JSON object in the form:

    1{
    2 "id": "HlGlbFdiqjb",
    3 "joke": "Why don't eggs tell jokes? They'd crack each other up",
    4 "status": 200
    5}

    We read aloud the joke property of the response JSON using the play method.

Sending POST request

Let us send some information to the server using a POST request.

1version: 1.0.0
2sections:
3 main:
4 - answer: {}
5 - prompt:
6 play: 'say: Enter PIN'
7 max_digits: 4
8 terminators: '#'
9 - request:
10 url: 'https://reqres.in/api/users'
11 method: POST
12 save_variables: true
13 body:
14 pin: '${prompt_value}'
15 - play:
16 url: 'say:Pin set to: ${request_response.pin}'
17 - hangup: {}

Using your own server

So far, we have used publicly available test REST API. You can also write your own web server, and interact with that using the request method.

An example server in Flask (Python) and Express (Node.js) is presented below:

1from flask import Flask, request
2from waitress import serve
3
4app = Flask(__name__)
5
6@app.route("/", methods=['POST'])
7def swml():
8 body = request.get_json(silent=True)
9 print(body)
10
11 # TODO: process body
12 result = body
13 return result
14
15if __name__ == "__main__":
16 serve(app, host='0.0.0.0', port=6000)

If you need help opening up this web server from your localhost to the wider internet where SWML execution system can find it, consider using a tool like ngrok to expose your localhost.

Once this web server is up, you can write SWML to access this service.

1version: 1.0.0
2sections:
3 main:
4 - answer: {}
5 - prompt:
6 play: 'say: Enter PIN'
7 max_digits: 4
8 terminators: '#'
9 - request:
10 url: 'https://<uuid>.ngrok-free.dev'
11 method: POST
12 save_variables: true
13 body:
14 pin: '${prompt_value}'
15 - play:
16 url: 'say:Pin set to: ${request_response.pin}'
17 - hangup: {}