@@ -10,24 +10,17 @@ components to send HTTP requests to certain hosts. This is configured in
1010> [ the Wasm examples] ( https://github.com/tinygo-org/tinygo/tree/release/src/examples/wasm ) .
1111
1212Creating and sending HTTP requests from Spin components closely follows the Go
13- ` net/http ` API:
14-
15- ``` go
16- r1 , err := spin_http.Get (" https://random-data-api.fermyon.app/animals/json" )
17- r2 , err := spin_http.Post (" https://postman-echo.com/post" , " text/plain" , bytes.NewBufferString (" Hello there!" ))
18-
19- req , err := http.NewRequest (" PUT" , " https://postman-echo.com/put" , bytes NewBufferString (" General Kenobi!" ))
20- req.Header .Add (" foo" , " bar" )
21- r3 , err := spin_http.Send (req)
22- ```
13+ ` net/http ` API. See [ tinygo-hello/main.go] ( ./tinygo-hello/main.go ) .
2314
2415Building this as a WebAssembly module can be done using the ` tinygo ` compiler:
2516
2617``` shell
27- $ go mod tidy
2818$ spin build
29- Executing the build command for component tinygo-hello: tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go
30- Successfully ran the build command for the Spin components.
19+ Building component outbound-http-to-same-app with ` tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go`
20+ Working directory: " ./outbound-http-to-same-app"
21+ Building component tinygo-hello with ` tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go`
22+ Working directory: " ./tinygo-hello"
23+ Finished building all Spin components
3124```
3225
3326The component configuration must contain a list of all hosts allowed to send
@@ -37,15 +30,26 @@ HTTP requests to, otherwise sending the request results in an error:
3730Cannot send HTTP request: Destination not allowed: <URL>
3831```
3932
33+ The ` tinygo-hello ` component has the following allowed hosts set:
34+
35+ ``` toml
36+ [component .tinygo-hello ]
37+ source = " tinygo-hello/main.wasm"
38+ allowed_http_hosts = [
39+ " https://random-data-api.fermyon.app" ,
40+ " https://postman-echo.com" ,
41+ ]
42+ ```
43+
44+ And the ` outbound-http-to-same-app ` uses the dedicated ` self ` keyword to enable making
45+ a request to another component in this same app, via a relative path (in this case, the component
46+ is ` tinygo-hello ` at ` /hello ` ):
47+
4048``` toml
41- [[component ]]
42- id = " tinygo-hello"
43- source = " main.wasm"
44- allowed_http_hosts = [ " https://random-data-api.fermyon.app" , " https://postman-echo.com" ]
45- [component .trigger ]
46- route = " /hello"
47- [component .build ]
48- command = " tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go"
49+ [component .outbound-http-to-same-app ]
50+ source = " outbound-http-to-same-app/main.wasm"
51+ # Use self to make outbound requests to components in the same Spin application.
52+ allowed_http_hosts = [" self" ]
4953```
5054
5155At this point, we can execute the application with the ` spin ` CLI:
@@ -58,11 +62,23 @@ The application can now receive requests on `http://localhost:3000/hello`:
5862
5963``` shell
6064$ curl -i localhost:3000/hello -X POST -d " hello there"
65+ HTTP/1.1 200 OK
66+ content-length: 976
67+ date: Thu, 26 Oct 2023 18:26:17 GMT
68+
69+ {{" timestamp" :1698344776965," fact" :" Reindeer grow new antlers every year" }}
70+ ...
71+ ```
6172
73+ As well as via the ` /outbound-http-to-same-app ` path to verify outbound http to the ` tinygo-hello ` component:
74+
75+ ``` shell
76+ $ curl -i localhost:3000/outbound-http-to-same-app
6277HTTP/1.1 200 OK
63- content-type: text/plain; charset=utf-8
64- content-length: 789
65- date: Wed, 09 Mar 2022 17:23:08 GMT
78+ content-length: 946
79+ date: Thu, 26 Oct 2023 18:26:53 GMT
80+
81+ {{{" timestamp" :1698344813408," fact" :" Some hummingbirds weigh less than a penny" }}
6682...
6783```
6884
0 commit comments