From fa446c659467c9a47fc93f20383291ba8d938b99 Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:50:07 -0700 Subject: [PATCH] add x-response-code add x-response-code --- main.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 6c75b94..a359f45 100644 --- a/main.go +++ b/main.go @@ -4,17 +4,27 @@ import ( "fmt" "net/http" "os" + "strconv" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - // method := r.Method - // path := r.URL.Path - responseHeader := r.Header["X-Response-Json"][0] - response := fmt.Sprint(responseHeader) + responseBody := r.Header["X-Response-Json"][0] + var responseCode int + if val, ok := r.Header["X-Response-Code"]; !ok { + responseCode = 200 + } else { + var err error + responseCode, err = strconv.Atoi(val[0]) + if err != nil { + fmt.Println("not a value status code: ", err) + } + } + + w.WriteHeader(responseCode) w.Header().Set("Content-Type", "application/json") - w.Write([]byte(response)) + w.Write([]byte(responseBody)) }) if err := http.ListenAndServe(":8080", nil); err != nil {