Skip to content

Commit

Permalink
add x-response-code
Browse files Browse the repository at this point in the history
add x-response-code
  • Loading branch information
notnmeyer committed Oct 18, 2023
1 parent 21cb4cb commit fa446c6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fa446c6

Please sign in to comment.