Skip to content

Instantly share code, notes, and snippets.

@pradumangoyal
Last active December 9, 2018 17:37
Show Gist options
  • Save pradumangoyal/d7e742422d16e3d7cd7b398b41f22188 to your computer and use it in GitHub Desktop.
Save pradumangoyal/d7e742422d16e3d7cd7b398b41f22188 to your computer and use it in GitHub Desktop.
... # Other imports
import requests
import get_client_ip from './utils.py' # See next snippet
secret_key = 'YOUR_SECRET_SITE_KEY' # Preferably from settings.py
class Submission(APIView):
...
def post(self, request, *args, **kwargs):
r = requests.post(
'https://www.google.com/recaptcha/api/siteverify',
data={
'secret': secret_key,
'response': request.data['g-recaptcha-response'],
'remoteip': get_client_ip(self.request), # Optional
}
)
if r.json()['success']:
# Successfuly validated
# Handle the submission, with confidence!
return self.create(request, *args, **kwargs)
# Error while verifying the captcha
return Response(data={'error': 'ReCAPTCHA not verified.'}, status=status.HTTP_406_NOT_ACCEPTABLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment