I'm working on two web applications (hereafter called App1 and App2) and both running on my Windows 10 PC. I am trying to make an https call from the backend of App2 and the backend of App1.
App1 is an application written in ASP.NET and runs directly on Windows 10. This application can be reached via browser at https://localhost:44311/
App2 is written in Django and runs in Ubuntu-20.04 through WSL2 on the same Windows computer. This application can be reached via browser at the address http://localhost:8000/
Initial Test
In App1 I expose a test function called GetMyResponse:
[HttpGet]
[AllowAnonymous]
[Route("GetMyResponse")]
public IActionResult GetMyResponse() {
return Ok("Hello World!");
}
I tried to connect to https://localhost:44311/GetMyResponse/ via web browser and everything seems to work correctly.
Attempt 1
I'm trying to make an https call from the backend of App2 to the backend of App1 like this:
import requests
class App1APIView(APIView):
def get(self, request, *args, **kwargs):
response = requests.get("https://localhost:44311/GetMyResponse/", verify=False)
return Response(response.content)
This gives me the following error (I tried to disable the windows Firewall but nothing has changed):
Traceback (most recent call last):
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/rest_framework/views.py", line 505, in dispatch
response = self.handle_exception(exc)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/rest_framework/views.py", line 465, in handle_exception
self.raise_uncaught_exception(exc)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
raise exc
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/rest_framework/views.py", line 502, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/d/Projects/app2/evar/sizing/api/views.py", line 74, in get
response = requests.get("https://localhost:44311/GetMyResponse/", verify=False)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "/mnt/d/Projects/app2/venv/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='localhost', port=44311): Max retries exceeded with url: /GetMyResponse/ (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f20694e8490>: Failed to establish a new connection: [Errno 111] Connection refused'))
Attempt 2
I tried to follow this solution: Access a localhost running in Windows from inside WSL2?
import requests
class App1APIView(APIView):
def get(self, request, *args, **kwargs):
response = requests.get("https://winhost:44311/GetMyResponse/", verify=False)
return Response(response.content)
At runtime I don't find any errors but the answer is the following:
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\"http://www.w3.org/TR/html4/strict.dtd\">\r\n<HTML><HEAD><TITLE>Bad Request</TITLE>\r\n<META HTTP-EQUIV=\"Content-Type\" Content=\"text/html; charset=us-ascii\"></HEAD>\r\n<BODY><h2>Bad Request - Invalid Hostname</h2>\r\n<hr><p>HTTP Error 400. The request hostname is invalid.</p>\r\n</BODY></HTML>\r\n"