Created
September 4, 2023 14:10
-
-
Save wakaztahir/88dc01e4e28e35d008892e85a0078e20 to your computer and use it in GitHub Desktop.
Verify Recaptcha Token In Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"encoding/json" | |
"net/http" | |
"net/url" | |
) | |
type RecaptchaResponse struct { | |
Success bool `json:"success"` | |
Hostname string `json:"hostname"` | |
ErrorCodes []string `json:"error-codes"` | |
} | |
func VerifyRecaptchaToken(token string, secret string) (*RecaptchaResponse, error) { | |
var response = &RecaptchaResponse{ | |
Success: false, | |
} | |
if len(token) != 0 { | |
//Preparing Recaptcha Request Parameters | |
data := url.Values{ | |
"secret": {secret}, | |
"response": {token}, | |
} | |
//Sending Request To Verify Recaptcha Token | |
reqResponse, err := http.PostForm("https://www.google.com/recaptcha/api/siteverify", data) | |
if err != nil { | |
return response, err | |
} | |
//Decoding Response Into Struct | |
decoder := json.NewDecoder(reqResponse.Body) | |
err = decoder.Decode(&response) | |
if err != nil { | |
return response, err | |
} | |
return response, nil | |
} else { | |
return response, nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment