Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions http-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down