diff --git a/README.md b/README.md index 97f916a..39930f1 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,39 @@ Simple server image that list information about the request in the response body } ``` +## Set Cookies +`curl -I -c cookies http://localhost:8080/setcookie?JSESSIONID=ABC123` + +``` +HTTP/1.1 200 OK +Set-Cookie: JSESSIONID=ABC123 +Date: Fri, 21 Apr 2023 11:36:48 GMT +``` + +`curl -b cookies http://localhost:8080 | jq .` + +```json +{ + "name": "server1", + "request": { + "url": "/", + "method": "GET", + "headers": { + "Accept": [ + "*/*" + ], + "Cookie": [ + "JSESSIONID=ABC123" + ], + "User-Agent": [ + "curl/7.87.0" + ] + } + } +} + +``` + ## Optional environment variables - NAME - Specify a server name added to the response body diff --git a/http-server.go b/http-server.go index 375456d..3785826 100644 --- a/http-server.go +++ b/http-server.go @@ -36,8 +36,17 @@ func info(w http.ResponseWriter, req *http.Request) { json.NewEncoder(w).Encode(body) } +func setCookie(w http.ResponseWriter, req *http.Request) { + fmt.Println("Set cookies:", req.URL.Query()) + + for k, v := range req.URL.Query() { + http.SetCookie(w, &http.Cookie{Name: k, Value: v[0]}) + } +} + func main() { http.HandleFunc("/", info) + http.HandleFunc("/setcookie", setCookie) fmt.Println("start", name()) http.ListenAndServe(":8080", nil)