CORS issue with HTTP requests when sending http get request to host IP

Question:

I have a development environment with python’s django web server running locally.

I had setup django to respond to API requests and whenever I send http GET requests to http://localhost:8000/api/service/ the server responds as expected.

The issue happens whenever I send the same request using the host IP, i.e, http://192.168.15.66:8000/api/service/ (I realized the issue when trying to get the api data from another host in the LAN)

I’m using postman to send the requests in order to validate whether it’s working or not, though, I’m developing a javascript react application that already consumes the api data.

Through the web javascript application, there’s a bit more information. The browser’s console presents the error message:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://192.168.15.66:8000/api/service/. (Reason: CORS request did not succeed). Status code: (null).

I’ve been searching around and tried a few suggestions, such as:

setting server/setting.py ALLOWED_HOSTS to:

  • [‘192.168.15.66’] , to [] and to [‘*’]

whitelisting with CORS_ORIGIN_WHITELIST to:

  • [‘192.168.15.66’] and to [‘*’]

Here’s my setting.py MIDDLEWARE varible value:

 MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
] 

Any clues what I could be missing?

Asked By: user1589169

||

Answers:

When running django, I had to pass the host IP along with the start script:

python manage.py runserver ip-addr:8000

This makes all the request from the network to be allowed by CORS rules.

Answered By: user1589169
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.