-
Simple usage with default values (httpClient: http.DefaultClient)
package main
import (
"fmt"
"xxx.engineer.com/gaptcha"
)
func main() {
rec, err := gaptcha.New(<secret>, gaptcha.WithVersion(2))
if err != nil {
panic(err)
}
if err = rec.Verify(<response>); err != nil {
panic(err)
}
fmt.Println("Success")
}
-
Simple usage with custom http client
package main
import (
"fmt"
"xxx.engineer.com/gaptcha"
)
func main() {
customClient := &http.Client{Timeout: time.Second * 10}
rec, err := gaptcha.New(
<secret>,
gaptcha.WithVersion(2),
gaptcha.WithHTTPClient(customClient)
)
if err != nil {
panic(err)
}
if err = rec.Verify(<response>); err != nil {
panic(err)
}
fmt.Println("Success")
}
-
Get reCAPTCHA token from request body (g-recaptcha-response
field)
import (
"fmt"
"net/http"
"xxx.engineer.com/gaptcha"
)
func Handler(w http.ResponseWriter, r *http.Request) {
rec, err := gaptcha.New(<secret>, gaptcha.WithVersion(2))
if err != nil {
panic(err)
}
response, err := rec.GetRequestToken(r)
if err != nil {
panic(err)
}
if err = rec.Verify(response); err != nil {
panic(err)
}
fmt.Println("Success")
}
-
Simple usage with default values (version: 3, action: "", score: 0.5, httpClient: http.DefaultClient)
package main
import (
"fmt"
"xxx.engineer.com/gaptcha"
)
func main() {
rec, err := gaptcha.New(<secret>)
if err != nil {
panic(err)
}
if err = rec.Verify(<response>); err != nil {
panic(err)
}
fmt.Println("Success")
}
-
Simple usage with custom values
package main
import (
"fmt"
"xxx.engineer.com/gaptcha"
)
func main() {
customClient := &http.Client{Timeout: time.Second * 10}
customAction := "custom-action"
customScore := 0.7
rec, err := gaptcha.New(
<secret>,
gaptcha.WithHTTPClient(customClient),
gaptcha.WithAction(customAction),
gaptcha.WithScore(customScore),
)
if err != nil {
panic(err)
}
if err = rec.Verify(<response>); err != nil {
panic(err)
}
fmt.Println("Success")
}
-
Get reCAPTCHA token from request body (g-recaptcha-response
field)
package main
import (
"fmt"
"net/http"
"xxx.engineer.com/gaptcha"
)
func Handler(w http.ResponseWriter, r *http.Request) {
rec, err := gaptcha.New(<secret>)
if err != nil {
panic(err)
}
response, err := rec.GetRequestToken(r)
if err != nil {
panic(err)
}
if err = rec.Verify(response); err != nil {
panic(err)
}
fmt.Println("Success")
}