github.com/Jeffail/benthos/v3@v3.65.0/website/docs/guides/streams_mode/using_rest_api.md (about) 1 --- 2 title: Streams Via REST API 3 --- 4 5 By using the Benthos `streams` mode REST API you can dynamically control which streams are active at runtime. The full spec for the Benthos streams mode REST API can be [found here][http-interface]. 6 7 Note that stream configs created and updated using this API do *not* benefit from [environment variable interpolation][interpolation] (function interpolation will still work). 8 9 ## Walkthrough 10 11 Start by running Benthos in streams mode: 12 13 ```bash 14 $ benthos streams 15 ``` 16 17 On a separate terminal we can add our first stream `foo` by `POST`ing a JSON or YAML config to the `/streams/foo` endpoint: 18 19 ```bash 20 $ curl http://localhost:4195/streams/foo -X POST --data-binary @- <<EOF 21 input: 22 http_server: {} 23 buffer: 24 memory: {} 25 pipeline: 26 threads: 4 27 processors: 28 - bloblang: | 29 root = { 30 "id": this.user.id, 31 "content": this.body.content 32 } 33 output: 34 http_server: {} 35 EOF 36 ``` 37 38 Now we can check the full set of streams loaded by `GET`ing the `/streams` endpoint: 39 40 ```bash 41 $ curl http://localhost:4195/streams | jq '.' 42 { 43 "foo": { 44 "active": true, 45 "uptime": 7.223545951, 46 "uptime_str": "7.223545951s" 47 } 48 } 49 ``` 50 51 And we can send data to our new stream via it's namespaced URL: 52 53 ``` 54 $ curl http://localhost:4195/foo/post -d '{"user":{"id":"foo"},"body":{"content":"bar"}}' 55 ``` 56 57 Good, now let's add another stream `bar` the same way: 58 59 ```bash 60 $ curl http://localhost:4195/streams/bar -X POST --data-binary @- <<EOF 61 input: 62 kafka: 63 addresses: 64 - localhost:9092 65 topics: 66 - my_topic 67 pipeline: 68 threads: 1 69 processors: 70 - bloblang: 'root = this.uppercase()' 71 output: 72 elasticsearch: 73 urls: 74 - http://localhost:9200 75 EOF 76 ``` 77 78 And check the set again: 79 80 ```bash 81 $ curl http://localhost:4195/streams | jq '.' 82 { 83 "bar": { 84 "active": true, 85 "uptime": 10.121344484, 86 "uptime_str": "10.121344484s" 87 }, 88 "foo": { 89 "active": true, 90 "uptime": 19.380582951, 91 "uptime_str": "19.380583306s" 92 } 93 } 94 ``` 95 96 It's also possible to get the configuration of a loaded stream by `GET`ing the path `/streams/{id}`: 97 98 ```bash 99 $ curl http://localhost:4195/streams/foo | jq '.' 100 { 101 "active": true, 102 "uptime": 30.123488951, 103 "uptime_str": "30.123488951s" 104 "config": { 105 "input": { 106 "http_server": { 107 "address": "", 108 "cert_file": "", 109 "key_file": "", 110 "path": "/post", 111 "timeout": "5s" 112 } 113 }, 114 "buffer": { 115 "memory": { 116 "limit": 10000000 117 } 118 }, 119 ... etc ... 120 } 121 } 122 ``` 123 124 Next, we might want to update stream `foo` by `PUT`ing a new config to the path `/streams/foo`: 125 126 ```bash 127 $ curl http://localhost:4195/streams/foo -X PUT --data-binary @- <<EOF 128 input: 129 http_server: {} 130 pipeline: 131 threads: 4 132 processors: 133 - bloblang: | 134 root = { 135 "id": this.user.id, 136 "content": this.body.content 137 } 138 output: 139 http_server: {} 140 EOF 141 ``` 142 143 We have removed the memory buffer with this change, let's check that the config has actually been updated: 144 145 ```bash 146 $ curl http://localhost:4195/streams/foo | jq '.' 147 { 148 "active": true, 149 "uptime": 12.328482951, 150 "uptime_str": "12.328482951s" 151 "config": { 152 "input": { 153 "http_server": { 154 "address": "", 155 "cert_file": "", 156 "key_file": "", 157 "path": "/post", 158 "timeout": "5s" 159 } 160 }, 161 "buffer": { 162 "type": "none" 163 }, 164 ... etc ... 165 } 166 } 167 ``` 168 169 Good, we are done with stream `bar` now, so let's delete it by `DELETE`ing the `/streams/bar` endpoint: 170 171 ```bash 172 $ curl http://localhost:4195/streams/bar -X DELETE 173 ``` 174 175 And let's `GET` the `/streams` endpoint to see the new set: 176 177 ```bash 178 $ curl http://localhost:4195/streams | jq '.' 179 { 180 "foo": { 181 "active": true, 182 "uptime": 31.872448851, 183 "uptime_str": "31.872448851s" 184 } 185 } 186 ``` 187 188 Great. Another useful feature is `POST`ing to `/streams`, this allows us to set the entire set of streams with a single request. 189 190 The payload is a map of stream ids to configurations and this will become the exclusive set of active streams. If there are existing streams that are not on the list they will be removed. 191 192 ```bash 193 $ curl http://localhost:4195/streams -X POST --data-binary @- <<EOF 194 bar: 195 input: 196 http_client: 197 url: http://localhost:4195/baz/get 198 output: 199 stdout: {} 200 baz: 201 input: 202 http_server: {} 203 output: 204 http_server: {} 205 EOF 206 ``` 207 208 Let's check our new set of streams: 209 210 ```bash 211 $ curl http://localhost:4195/streams | jq '.' 212 { 213 "bar": { 214 "active": true, 215 "uptime": 3.183883444, 216 "uptime_str": "3.183883444s" 217 }, 218 "baz": { 219 "active": true, 220 "uptime": 3.183883449, 221 "uptime_str": "3.183883449s" 222 } 223 } 224 ``` 225 226 Done. 227 228 [http-interface]: /docs/guides/streams_mode/streams_api 229 [interpolation]: /docs/configuration/interpolation