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
• 15,510 points
1,868 views

1 answer to this question.

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
• 58,020 points

Related Questions In AWS

0 votes
2 answers

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

A curl or wget to http://169.254.169.254/latest/meta-data/instance-id will ...READ MORE

answered Mar 9, 2020 in AWS by anonymous
6,925 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP