Link Search Menu Expand Document

HTTP requests

Sometimes you need information, data, from an external service on the web. This could be local weather information, currency or stock prices or the status of a server. There are also “webhooks”, small endpoints that can receive requests and trigger an action on a service, for instance, post a message on a Teams or Slack channel, enter data into a database, or even purchase something online. How to do this from within the OOCSI network?

The HTTP Web Request client

The OOCSI server has a client running in the background that carries out web requests. You can send messages to this client “http-web-request”. For instance in Python:

oocsi.send('http-web-request', message)

GET requests

The message is where the magic happens:

# prepare the data
message = {}
message['url'] = 'https://example.org/api/weather/Amsterdam?token=123456789'
message['method'] = 'get'
# send the request
oocsi.send('http-web-request', message)

Above you can see how to send a GET request to a web address (in url). The result of this GET request will be sent back to the client that send the message.

POST requests

Some web services need a POST request to properly work. Not a problem, just change the method field:

# prepare the data
message = {}
message['url'] = 'https://example.org/api/weather/Amsterdam?token=123456789'
# change this field to send a POST request
message['method'] = 'post'
# post JSON data to the web service
message['json'] = '{"data": 1, "transmit": true}'
# send the request
oocsi.send('http-web-request', message)

The request above is sent as a POST request to the URL with the JSON payload {"data": 1, "transmit": true}. You can also send the POST request in the HTML Form URL encoded format:

# prepare the data
message = {}
message['url'] = 'https://example.org/api/weather/Amsterdam?token=123456789'
# change this field to send a POST request
message['method'] = 'post'
# post Form URL Encoded data to the web service
message['body'] = 'data=1&transmit=true'
# send the request
oocsi.send('http-web-request', message)

Again, the response is sent back to the client that sends the request to http-web-request.

Routing responses to a channel

Receiving the response back in the client is not always the best choice. You can add a channel attribute in the message to “route” the response to a channel of your choice:

# prepare the data
message = {}
message['url'] = 'https://example.org/api/weather/Amsterdam?token=123456789'
message['method'] = 'post'
message['body'] = 'data=1&transmit=true'

# route the response to the channel web_response_channel
message['channel'] = 'web_response_channel'

oocsi.send('http-web-request', message)

Obviously, all these requests also work any other client implementation (not just Python).


Copyright © 2013-2024 Mathias Funk. test