i have started exploring api stuffs in django
i want some JsonResponse in Django but i am not getting it
views.py
from django.shortcuts import render
from django.http import JsonResponse
import json
# Create your views here.
def api_home(request, *args, **kwargs):
body = request.body
print(body)
data = {}
try:
data = json.loads(body)
except:
pass
# print(data)
data['headers'] = dict(request.headers)
data['content_type'] = request.content_type
return JsonResponse(data)
Output in terminal
b''
expected OUTPUT
b'{"query":"Hello World!"}'
Python file
import requests
endpoint = "http://127.0.0.1:8000/api"
get_response = requests.get(endpoint, json={"query":"Hello World!"})
print(get_response.json())
Output of this file
{'headers': {'Content-Length': '', 'Content-Type': 'text/plain', 'Host':
'127.0.0.1:8000', 'User-Agent': 'python-requests/2.27.1', 'Accept-Encoding': 'gzip,
deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}, 'content_type': 'text/plain'}
Expected OUTPUT
{'query':'Hello World!','headers': {'Content-Length': '', 'Content-Type': 'text/plain',
'Host':'127.0.0.1:8000', 'User-Agent': 'python-requests/2.27.1', 'Accept-Encoding':
'gzip,deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}, 'content_type':
'text/plain'}
I am not getting that why i am getting different output here
link entered was missing a '/' at end
endpoint = "http://127.0.0.1:8000/api/"