1

I have a problem with my Django code :

I tried this :

requests.post('https://localhost:8000/api/test/', data=data, headers={'Content-Type': 'application/json'}, verify=False)

But I got this :

{SSLError}HTTPSConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /api/test/ (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)'),))

I achieved to solve the problem using HTTP instead of https but I want to use https.

How can I do this knowing that all of this is on the localhost ?

Thank you very much!

EDIT :

Here is the urls.py from the api app :

from django.urls import path, include
from django.views.decorators.csrf import csrf_exempt
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token

from API import views as API views

app_name = 'api'


from API.views import LoginViewCustom

urlpatterns = [
path('test/', apiviews.Test.as_view(), name='test')
]

and in the other urls.py :

urlpatterns = [path('api/', include('api.urls', namespace='api'))]
Eb Heravi
  • 398
  • 5
  • 15
Peter
  • 11
  • 4
  • Please show your **urls.py** – Lewis Aug 18 '20 at 09:09
  • You must have a certificate to use HTTPS, do you have it? You can create one for the localhost, see the Let's Encrypt docs: https://letsencrypt.org/docs/certificates-for-localhost/. One more tool, which can help you is the ngrok, you can make a tunnel for the localhost with both HTTP and HTTPS. – Alexander Yudkin Aug 18 '20 at 09:38

1 Answers1

1

Django's development server, by default run the application over http. Therefore you are getting Max retries exceeded with url error as you try to reach it via https.

Therefore what you actually want is to test/run the local development server with SSL/HTTPS.

There are several way to achieve this, But what I prefer is to use RunServerPlus from django-extensions. They have this section for SSL setup.

You can find a good discussion in this question.

Dharman
  • 30,962
  • 25
  • 85
  • 135
arifin4web
  • 696
  • 8
  • 12
  • I tried to install RunServerPlus but I got the following error : `"from django.utils.autoreload import get_reloader` `ImportError: cannot import name 'get_reloader'"` when I launched the server using this command : `./manage.py runserver_plus --cert /tmp/cert` – Peter Aug 18 '20 at 14:56
  • Actually, it seems appears that there is no the function `get_reloader()` into the file autoreload – Peter Aug 18 '20 at 14:58
  • What version of Django are you using? – arifin4web Aug 19 '20 at 07:43