Home > Mobile >  HTTPResponse.__init__() got an unexpected keyword argument 'content_type'
HTTPResponse.__init__() got an unexpected keyword argument 'content_type'

Time:08-22

Error -

TypeError at /api/ HTTPResponse.init() got an unexpected keyword argument 'content_type' Request Method: GET Request URL: http://127.0.0.1:8000/api/ Django Version: 4.0.5 Exception Type: TypeError Exception Value:
HTTPResponse.init() got an unexpected keyword argument 'content_type' Exception Location: D:\Python_Tutorials\DJango\PracticeDjango\Rest_Practice\withoutrest\testapp\views.py, line 17, in emp_data_json_view Python Executable: C:\Users\HP\AppData\Local\Programs\Python\Python310\python.exe Python Version: 3.10.5 Python Path:
['D:\Python_Tutorials\DJango\PracticeDjango\Rest_Practice\withoutrest', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\python310.zip', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\DLLs', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\lib', 'C:\Users\HP\AppData\Local\Programs\Python\Python310', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\site-packages', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\site-packages\win32', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\site-packages\win32\lib', 'C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\site-packages\Pythonwin'] Server time: Sat, 20 Aug 2022 17:29:03 0000

from http.client import HTTPResponse
from django.shortcuts import render
import json
# Create your views here.
def emp_data_json_view(request):
    emp_data = {'eno': 100,
                'ename':'sunny',
                'esal' : 1000,
                'address' : 'Pune'
                }

    # dumps() -> coonvert python dict to json
    # loads() -> convert json to python dict
    #converting pytohn dictionary to json data

    json_data = json.dumps(emp_data)
    # if we did not mention content type - it will be consider as HTTP response only
    return HTTPResponse(json_data, content_type = 'application/json')

CodePudding user response:

I made changes to code and used JsonResponse directly than HTTPRespons And that worked for me.

from django.http import JsonResponse
def emp_data_json_view(request):
    emp_data = {'eno': 100,
                'ename':'sunny',
                'esal' : 1000,
                'address' : 'Pune'
                }
    return JsonResponse(emp_data)

CodePudding user response:

Wrong import caused me this error -

 Replacing this - from http.client import HTTPResponse
    to this -     from django.http import HttpResponse

#from http.client import HTTPResponse
from django.http import HttpResponse
from django.shortcuts import render
import json
# Create your views here.
def emp_data_json_view(request):
    emp_data = {'eno': 100,
                'ename':'sunny',
                'esal' : 1000,
                'address' : 'Pune'
                }

    # dumps() -> coonvert python dict to json
    # loads() -> convert json to python dict
    #converting pytohn dictionary to json data

    json_data = json.dumps(emp_data)
    # if we did not mention content type - it will be consider as HTTP response only
    return HttpResponse(json_data, content_type = 'application/json')
  • Related