Getting error while sending a POST request to an API in Django.

The problem: I am using Daphne with Django 4.0. I have created a functional view using async,

async def inputView(request):
    print("Guess where I am")
    return HttpResponse("Well, I recieved the file.")

and I am sending a POST request with a file using CURL to this view and just returning an HTTPResponse but still it is giving this error:

2022-04-01 06:05:29,969 INFO Starting server at tcp:port=8000:interface=127.0.0.1
2022-04-01 06:05:29,970 INFO HTTP/2 support enabled
2022-04-01 06:05:29,970 INFO Configuring endpoint tcp:port=8000:interface=127.0.0.1
2022-04-01 06:05:29,970 INFO Listening on TCP address 127.0.0.1:8000
Internal Server Error: /primerDesign/uploadInput
Traceback (most recent call last):
File “/home/biord002/im-lab/lib/python3.8/site-packages/asgiref/sync.py”, line 451, in thread_handler
raise exc_info[1]
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 38, in inner
response = await get_response(request)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 243, in _get_response_async
self.check_response(response, callback)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 314, in check_response
raise ValueError(
ValueError: The view primerDesign.views.inputView didn’t return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an ‘await’ into your view.
2022-04-01 06:05:34,930 ERROR Internal Server Error: /primerDesign/uploadInput
Traceback (most recent call last):
File “/home/biord002/im-lab/lib/python3.8/site-packages/asgiref/sync.py”, line 451, in thread_handler
raise exc_info[1]
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 38, in inner
response = await get_response(request)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 243, in _get_response_async
self.check_response(response, callback)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 314, in check_response
raise ValueError(
ValueError: The view primerDesign.views.inputView didn’t return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an ‘await’ into your view.
127.0.0.1:34482 - - [01/Apr/2022:06:05:34] “POST /primerDesign/uploadInput” 500 59356
127.0.0.1:34484 - - [01/Apr/2022:06:06:25] “GET /primerDesign/” 200 47
Internal Server Error: /primerDesign/uploadInput
Traceback (most recent call last):
File “/home/biord002/im-lab/lib/python3.8/site-packages/asgiref/sync.py”, line 451, in thread_handler
raise exc_info[1]
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 38, in inner
response = await get_response(request)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 243, in _get_response_async
self.check_response(response, callback)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 314, in check_response
raise ValueError(
ValueError: The view primerDesign.views.inputView didn’t return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an ‘await’ into your view.
2022-04-01 06:06:49,191 ERROR Internal Server Error: /primerDesign/uploadInput
Traceback (most recent call last):
File “/home/biord002/im-lab/lib/python3.8/site-packages/asgiref/sync.py”, line 451, in thread_handler
raise exc_info[1]
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/exception.py”, line 38, in inner
response = await get_response(request)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 243, in _get_response_async
self.check_response(response, callback)
File “/home/biord002/im-lab/lib/python3.8/site-packages/django/core/handlers/base.py”, line 314, in check_response
raise ValueError(
ValueError: The view primerDesign.views.inputView didn’t return an HttpResponse object. It returned an unawaited coroutine instead. You may need to add an ‘await’ into your view.
127.0.0.1:34488 - - [01/Apr/2022:06:06:49] “POST /primerDesign/uploadInput” 500 59356

The curl request is this:

time curl -v -X POST -F “FILE=@/home/biord002/file.txt” http://127.0.0.1:8000/primerDesign/uploadInput

I have another view, index, with same structure but I am sending a GET request and that view works just fine.
My urls.py:

urlpatterns = [
    path("", index),
    path("uploadInput", csrf_exempt(inputView)),
]

index view:

async def index(request):
    return HttpResponse("hello bruh, I am done.")

Thanks in advance.:slight_smile:

Well, you’ve got three different variables between those two examples you’ve provided. I’d say the first step toward identifying a cause of the issue would be to identify what is different between those views that may be causing the problem.

If you want to do some narrowing down of the issues, you could try:

  • Post to your index view instead of get.
  • Post a file to that index view.
  • Do a get on the inputView
1 Like

The problem seems to be with the decorator csrf_exempt, but if I remove the decorator then there is another error

Forbidden (CSRF token missing.): /xyz/uploadInput
2022-04-05 07:33:26,541 WARNING Forbidden (CSRF token missing.): /xyz/uploadInput
127.0.0.1:44114 - - [05/Apr/2022:07:33:26] “POST /xyz/uploadInput” 403 2506

But, I have tried making a form with tag {%csrf_token%} and it works just fine.

I have tried removing the csrf middleware from the settings.py file and it seems to work fine.

Yes, if you’re trying to submit a POST through curl, you would need to include the csrf_token in the submit, along with using curl’s cookie manager to properly manage the cookies.
You could do a get for your login page - that’ll create the initial cookies and give you a page containing the appropriate csrf_token - then use that information in the POST.

Thanks for answering.