Reputation: 5075
I'm new with Django, so please bear with me.
I have the following view
@csrf_exempt
@api_view(http_method_names=['POST'])
def login_agro_user(request):
request_data = request.data
if not request_data:
return Response({"status_code": 400})
secret = request_data.get('secret')
if secret != 'test':
return Response({"status_code": 400})
payload = request_data.get('payload')
payload = json.loads(base64.b64decode(payload).decode('utf-8'))
serializer = AgroUserSerializer(data=payload)
if serializer.is_valid():
query = AgroUser.objects.filter(user_guid=payload['user_guid'])
if not query:
serializer.save()
elif query[0].active == False:
query.update(active=True)
key = secrets.token_urlsafe()
query.update(key=key)
response_data = {"callback": f"{settings.FRONT_BASE_URL}/{key}"}
return Response(response_data)
return Response({"status_code": 400})
This view seems to be linked to the following endpoint
urlpatterns = [
path('', admin.site.urls),
url(r'^api/v1/login_agro_user', login_agro_user),
The code should be generating a token, like this one zLdu6NrHnvyUnixnvV-PiaQwro4QSNp0MaRmYQ9W09c
.
However,
My question are the following:
Any help is welcomed as I'm losing my sanity.
Thanks.
Upvotes: 0
Views: 40
Reputation: 11626
Not sure this is the answer, but you can call your method like this:
curl -XPOST 0:8000/api/v1/login_agro_user -H "Content-Type: application/json" -d '{"secret": "test", "payload": "eyJmaXJzdF9uYW1lIjogIkpvaG4iLCAibGFzdF9uYW1lIjogIkRvZSIsICJlbWFpbCI6ICJmb29AZXhhbXBsZS5jb20iLCAiYWN0aXZlIjogdHJ1ZSwgImtleSI6ICJzZWNyZXQifQ=="}'
To create a proper payload you may use:
>>> import base64
>>> import json
>>> base64.b64encode(json.dumps({"first_name": "John", "last_name": "Doe", "email": "[email protected]", "active": True, "key": "secret"}).encode('utf8'))
b'eyJmaXJzdF9uYW1lIjogIkpvaG4iLCAibGFzdF9uYW1lIjogIkRvZSIsICJlbWFpbCI6ICJmb29AZXhhbXBsZS5jb20iLCAiYWN0aXZlIjogdHJ1ZSwgImtleSI6ICJzZWNyZXQifQ=='
Upvotes: 1