You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+20-13Lines changed: 20 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# OpenWhisk Building Block - HTTP REST Trigger
1
+
# OpenWhisk Building Block - HTTP REST API Trigger
2
2
Create REST API mappings with Apache OpenWhisk on IBM Bluemix. This tutorial takes less than 5 minutes to complete. After this, move on to more complex serverless applications such as those tagged [_openwhisk-hands-on-demo_](https://github.com/search?q=topic%3Aopenwhisk-hands-on-demo+org%3AIBM&type=Repositories).
@@ -12,7 +12,10 @@ This example provides two REST endpoints, HTTP `POST` and `GET` methods that are
12
12
4.[Clean up](#3-clean-up)
13
13
14
14
# 1. Create OpenWhisk actions
15
-
Create a file named `create-cat.js`. This file will define an OpenWhisk action written as a JavaScript function. It checks for the required parameters(`name` and `color`) and returns 201, or an error if either parameter is missing. This example is simplified, and does not connect to a backend datastore. For a more complicated example, check out this [REST API example](https://github.com/IBM/openwhisk-serverless-apis).
15
+
## Create an action to create a cat entity
16
+
Create a file named `create-cat.js`. This file will define an OpenWhisk action written as a JavaScript function. It checks for the required parameters(`name` and `color`) and returns a unique identifier for the cat, or an error if either parameter is missing.
17
+
> **Note**: This example is simplified, and does not connect to a backend datastore. For a more sophisticated example, check out this [REST API example](https://github.com/IBM/openwhisk-serverless-apis).
18
+
16
19
```javascript
17
20
functionmain(params) {
18
21
@@ -33,7 +36,10 @@ function main(params) {
33
36
}
34
37
```
35
38
36
-
Create a file named `fetch-cat.js`. This file will define an OpenWhisk action written as a JavaScript function. It checks for the required parameter(`id`) and returns Tahoma, the tabby colored cat. Again, for the purpose of this simplified demo we always return Tahoma the cat, rather than connecting to a backend datastore.
39
+
## Create an action to return a cat entity
40
+
Create a file named `fetch-cat.js`. This file will define an OpenWhisk action written as a JavaScript function. It checks for the required parameter(`id`) and returns Tahoma, the tabby colored cat.
41
+
> **Note**: Again, for the purpose of this simplified demo we always return Tahoma the cat, rather than connecting to a backend datastore.
42
+
37
43
```javascript
38
44
functionmain(params) {
39
45
@@ -56,14 +62,14 @@ function main(params) {
56
62
}
57
63
```
58
64
59
-
## Upload actions and test
60
-
The next step will be to create OpenWhisk actions from the JavaScript functions that we just created. To create an action, use the wsk CLI command: `wsk action create [action name] [JavaScript file]`
65
+
## Upload the actions
66
+
The next step will be to create OpenWhisk actions from the JavaScript functions that we just created. We also add the `--web true` flag, to annotate these actions as "Web Actions". This will be necessary later when we add REST endpoints.
We've also added the flag, `--web true`, to annotate these actions as "Web Actions". This will be necessary later when we add REST endpoints.
66
71
72
+
## Unit test the actions
67
73
OpenWhisk actions are stateless code snippets that can be invoked explicitly or in response to an event. For right now, we will test our actions by explicitly invoking them. Later, we will trigger our actions in response to an HTTP request. Invoke the actions using the code below and pass the parameters using the `--param` command line argument.
68
74
69
75
```bash
@@ -83,23 +89,24 @@ wsk action invoke \
83
89
84
90
# 2. Create REST endpoints
85
91
## Create POST and GET REST mappings for `/v1/cat` endpoint
86
-
Now that we have our OpenWhisk actions created, we will expose our OpenWhisk actions through the OpenWhisk API Gateway. To do this we will use: `wsk api create ([BASE_PATH] API_PATH API_VERB ACTION] [API PATH]`
92
+
Now that we have our OpenWhisk actions created, we will expose our OpenWhisk actions through the Bluemix API Gateway. To do this we will use: `wsk api create $BASE_PATH $API_PATH $API_VERB $ACTION`
87
93
88
-
This feature is part of the "Bluemix Native API Management" feature and currently supports very powerful API management features like security, rate limiting, and more. For now though we're just using the CLI to expose our action with a REST endpoint. You can read more about this feature here: [API Gateway](https://console.ng.bluemix.net/docs/openwhisk/openwhisk_apigateway.html#openwhisk_apigateway).
94
+
This feature is part of the [Bluemix Native API Management](https://console.ng.bluemix.net/docs/openwhisk/openwhisk_apigateway.html#openwhisk_apigateway) service and currently supports very powerful API management features like security, rate limiting, and more. For now though we're just using the CLI to expose our action with a public REST endpoint.
89
95
90
96
```bash
97
+
# Send along credentials with the command or provide them interactively
# POST /v1/cat {"name": "Tahoma", "color": "Tabby"}
100
+
#Exposes POST /v1/cat {"name": "Tahoma", "color": "Tabby"}
94
101
wsk api create -n "Cats API" /v1 /cat post create-cat
95
102
96
-
#GET /v1/cat?id=1
103
+
#Exposes /v1/cat?id=1
97
104
wsk api create /v1 /cat get fetch-cat
98
105
```
99
-
In both cases, the CLI will output the URL required to use the API. Make note of those URLs!
106
+
In both cases, the CLI will output the URL required to use the API. Make note of it for the next section.
100
107
101
108
## Test with `curl` HTTP requests
102
-
Take note of the API URL that is generated from the previous command. Send an http POST and GET request using CuRL to test the actions. Remember to send the required parameters in the body of the request for POST, or as path parameters for GET. OpenWhisk automatically forwards these parameters to the actions we created.
109
+
Take note of the API URL that is generated from the previous command. Send an http POST and GET request using `curl` to test the actions. Remember to send the required parameters in the body of the request for POST, or as path parameters for GET. OpenWhisk automatically forwards these parameters to the actions we created.
103
110
104
111
```bash
105
112
# POST /v1/cat {"name": "Tahoma", "color": "Tabby"}
0 commit comments