I am Unable to call Django REST API from within the same AWS EC2 Linux machine.

0 votes

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.

Sep 3, 2018 in AWS by bug_seeker
• 14,960 points
82 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
As per discussions in comment section, it's clear that inbound and outbound are fine. So, check the proxy settings like 'no_proxy' env variable etc. in your aws linux box itself.

command to check env variables: set

As it's allowing outbound and inbound, then try to set no_proxy value with appending your IP Address to it.

Please let me know if this helped.

Thanks.
answered Sep 3, 2018 by Priyaj
• 56,100 points

Related Questions In AWS

0 votes
1 answer

How can I find out the instance id of an ec2 instance from within the ec2 instance?

On Amazon Linux AMIs you can do: $ ...READ MORE

answered Sep 10, 2018 in AWS by Archana
• 3,770 points
14 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.