Arcaptcha library implementation in Golang to verify captcha.
go get -u github.com/arcaptcha/arcaptcha-go
Register on Arcaptcha, create website and get your own SiteKey and SecretKey
website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
//'arcaptcha-response' is created for each captcha
//After you put captcha widget in your website, you can get 'arcaptcha-response' from form
result, err := website.Verify("arcaptcha-response")
if err != nil {
// error in sending or receiving API request
// handle error
}
if !result.Success {
// captcha not verified
// can see result.ErrorCodes to find what's wrong
// throw specific error
}
// it's ok
package main
import (
"net/http"
"github.com/arcaptcha/arcaptcha-go"
)
var website *arcaptcha.Website
func main() {
website = arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
myHandler := http.HandlerFunc(handler)
http.Handle("/", verifyCaptcha(myHandler))
http.ListenAndServe(":8000", nil)
}
func handler(w http.ResponseWriter, r *http.Request) {
// handle request
}
func verifyCaptcha(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
response := r.FormValue("arcaptcha-response")
result, err := website.Verify(response)
if err != nil {
// error in sending or receiving API request
// handle error
}
if !result.Success {
// captcha not verified
// can see result.ErrorCodes to find what's wrong
// throw specific error
return
}
// it's ok
next.ServeHTTP(w, r)
})
}
you can set timeout
for verify request:
website := arcaptcha.NewWebsite("YOUR_SITE_KEY", "YOUR_SECRET_KEY")
website.SetTimeout(1*time.Second)
result, err := website.Verify("arcaptcha-response") // returns "context deadline" error if it takes more than 1 second