Skip to content

Instantly share code, notes, and snippets.

@mejutoco
Created February 12, 2021 15:13
Show Gist options
  • Save mejutoco/2fff1b3a356f59f55aedf33b7f868422 to your computer and use it in GitHub Desktop.
Save mejutoco/2fff1b3a356f59f55aedf33b7f868422 to your computer and use it in GitHub Desktop.
# views.py
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import requests
from django.utils.decorators import method_decorator
from django.views import View
@method_decorator(csrf_exempt, name='dispatch')
class AirtableProxy(View):
airtable_base = 'YOUR_BASE_ID'
# the name of your table
airtable_table = 'email_subscribers'
headers = {
"Authorization": 'Bearer YOUR_API_KEY',
"Content-Type": 'application/json'
}
@csrf_exempt
def post(self, request):
url = 'https://api.airtable.com/v0/{base}/{table}'.format(
base=self.airtable_base,
table=self.airtable_table,
)
if request.method == self.airtable_method:
email = request.POST.get('email', '')
if email != '':
data = {'fields': {'email': email}, 'typecast': True}
r = requests.post(url, json=data, headers=self.headers)
return JsonResponse({'ok': 1})
return JsonResponse({'ko': 1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment