This example illustrates how to read and write HTTP cookies from a Goa service.
The session service exposes two methods: create_session and use_session.
create_session writes a cookie to the HTTP response and use_session reads
it back.
The available DSL functions used to manipulate HTTP cookies are:
The following shows the DSL used to write the cookie:
HTTP(func() {
POST("/")
Response(StatusOK, func() {
Cookie("session_id:SID") // Return session ID in "SID" cookie
CookieMaxAge(3600) // Sessions last one hour
})
})The value of the HTTP cookie SID is read from the session_id field of the
method result object.
The extract below shows the DSL used to read the cookie:
HTTP(func() {
GET("/")
Cookie("session_id:SID") // Load session ID from "SID" cookie
Response(StatusOK)
})Build the server and the client:
go build ./cmd/session && go build ./cmd/session-cli
Run the server:
./session --http-port 8080
Run the client to create a session:
./session-cli -v -url http://localhost:8080 session create-session --body '{"name":"foo"}'
Run the client to use a session:
./session-cli -url http://localhost:8080 session use-session -session-id abcd