Web Actions
Web actions are actions which can be called externally using the HTTP protocol from clients like curl or web browsers. IBM Cloud Functions provides a simple flag, --web, which will cause it to automatically create an HTTP accessible URL (endpoint) for any action.
Let's turn the hello action into a "web action"!
1. Update the action to set the --web flag to true.
ibmcloud fn action update hello --web true2. Retrieve the web action's URL exposed by the platform for the hello action.
ibmcloud fn action get hello --url ok: got action hello
https://us-south.functions.cloud.ibm.com/api/v1/web/2ca6a304-a717-4486-ae33-1ba6be11a393/default/hello3. Invoke the web action URL returned using the curl command.
curl "https://us-south.functions.cloud.ibm.com/api/v1/web/2ca6a304-a717-4486-ae33-1ba6be11a393/default/hello"It looks like nothing happened! In fact, an HTTP response code of 204 No Content was returned. This is because we need to tell IBM Cloud Functions what content-type we expect the function to return.
4. Invoke the web action URL with a JSON extension using the curl command.
To do this we need to add `.json` after the action name, at the end of the URL, to tell ICF we want a JSON object returned. Try it invoking it now:
```bash
curl "https://us-south.functions.cloud.ibm.com/api/v1/web/2ca6a304-a717-4486-ae33-1ba6be11a393/default/hello.json"
```
We now get a successful HTTP response in JSON format:
```json
{
"message": "Hello undefined from Rivendell!"
}
```
Additionally, we can invoke it with query parameters for `name`:
```bash
curl "https://us-south.functions.cloud.ibm.com/api/v1/web/2ca6a304-a717-4486-ae33-1ba6be11a393/default/hello.json?name=Arwen"
```
```json
{
"message": "Hello Arwen from Rivendell!"
}
```Content extensions
Web actions invoked through the platform API need a content extension to tell the platform how to interpret the content returned from the action. In the example above, we were using the .json extension. This tells the platform to serialize the return value out to a JSON response.
Example: SVG Response
Create a new function that has the SVG base64 encoded as the body
Get the URL for the new atom web action
Copy and paste that URL into your browser to see the image!
Last updated
Was this helpful?