I am working on a Django REST Framework web application, for that I have a Django server running in an AWS EC2 Linux box at a particular IP:PORT. There are URLs (APIs) which I can call for specific functionalities.
In Windows machine as well as in other local Linux machine (not AWS EC2) I am able to call those APIs successfully and getting the desired results perfectly.
But the problem is when I am trying to call the APIs from within the same EC2 Linux box.
A simple code I wrote to test the call of one API from the same AWS EC2 Linux box:
import requests
vURL = 'http://<ipaddress>:<port>/myapi/'
vSession = requests.Session()
vSession.headers = {'Content-Type': 'application/json', 'Accept': 'application/json'}
vResponse = vSession.get(vURL)
if vResponse.status_code == 200:
print('JSON: ', vResponse.json())
else:
print('GET Failed: ', vResponse)
vSession.close()
This script is returning GET Failed: <Response [403]>.
In one thing I am sure that there is no authentication related issues in the EC2 instance because using this same script I got actual response in other local Linux machines (not AWS EC2) and also in Windows machine.
It seems that the calling of the API (which includes the same IP:PORT of the same AWS EC2 machine) from the same machine is somehow getting restricted by either the security policies of AWS or firewall or something else.
May be I have to do some changes in setting.py. Though I have incorporated all the required settings as per my knowledge in the settings.py, like:
- ALLOWED_HOST
- CORS_ORIGIN_WHITELIST
- Mentioning corsheaders in INSTALLED_APPS list
- Mentioning corsheaders.middleware.CorsMiddleware in MIDDLEWARE list
For example, below are the CORS settings that I have incorporated in the setting.py:
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_METHODS = ('GET', 'PUT', 'POST', 'DELETE')
CORS_ORIGIN_WHITELIST = (
< All the IP address that calls this
application are listed here,
this list includes the IP of the
AWS EC2 machine also >
)
Does anyone have any ideas regarding this issue? Please help me to understand the reason of this issue and how to fix this.
Thanks in advance.