package main import ( "encoding/json" "errors" "fmt" "io" "log" "net/http" "strings" ) func checkSolution(solution, apiKey string) error { req, err := http.NewRequest("POST", "https://api.privatecaptcha.com/verify", strings.NewReader(solution)) if err != nil { return err } req.Header.Set("X-Api-Key", apiKey) resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer resp.Body.Close() response := struct { Success bool `json:"success"` // NOTE: other fields omitted for brevity }{} if body, err := io.ReadAll(resp.Body); err == nil { fmt.Println(string(body)) if err = json.Unmarshal(body, &response); err != nil { return err } } if !response.Success { return errors.New("solution is not correct") } return nil } func main() { const page = `` http.HandleFunc("POST /submit", func(w http.ResponseWriter, r *http.Request) { captchaSolution := r.FormValue("private-captcha-solution") if err := checkSolution(captchaSolution, "pc_1c582dd06cde41b48e86f5cac278abb8"); err != nil { fmt.Fprintf(w, page, "red") return } fmt.Fprintf(w, page, "green") }) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { http.ServeFile(w, r, "index.html") return } // Return 404 for any other paths http.NotFound(w, r) }) if err := http.ListenAndServe(":8081", nil); err != nil { log.Fatal(err) } }