How to resolve Forbidden 403 error when making a urllib request with POST data in Django using Docker?

I have a three layer Django project that uses Docker. The first container is the front end and does all of the rendering, the second layer deals with logic for what to display on the front end and interacts with the database layer (third) by requesting JSON, and the third container/layer interacts directly with the database.

I have a form in the front end where a user tries to sign up. However, when I make an API call to my second layer using POST and passing the form values, I keep receiving a 403 Forbidden Error. I am using a CSRF token in my form. How can I remove the 403 Forbidden Error?

  • Running python program on docker
  • Getting error status trying to upload my kubernetes pod
  • Fail over with Docker Compose, two approaches
  • docker Error response from daemon: json: cannot unmarshal object into Go value of type []string
  • sudo command not found after trying to use docker without sudo
  • Accessing Elastic Beanstalk environment properties in Docker
  • Some code in my views.py at the front end level:

    def signup(request):
          #more code
          username = form.cleaned_data["username"] 
          #and some more fields
          data = {"username": username, "password":password, ... "location": location}
          postData = urllib.parse.urlencode(data).encode('utf-8')
          try:
             req = urllib.request.Request("url to API in 2nd layer", postData)
          #more code
    

    Django attempts to execute the try statement, but I am getting a 403 Forbidden Error.

  • Jenkins docker setup
  • Docker compose: is it possible to mount a local file to a remote daemon?
  • Docker login: cannot input value for username on windows + powershell
  • Docker secret not working for MYSQL_ROOT_PASSWORD_FILE
  • php-fpm & nginx in separated docker container, can not execute .php file
  • How to allow my host IP address in the PostgreSQL pg_hba.conf file
  • One Solution collect form web for “How to resolve Forbidden 403 error when making a urllib request with POST data in Django using Docker?”

    You need to allow CORS calls in your 2nd Django project

    pip install django-cors-headers
    

    …..

    INSTALLED_APPS = (
        ...
        'corsheaders',
        ...
    )
    

    …..

    MIDDLEWARE_CLASSES = [
        ...
        'corsheaders.middleware.CorsMiddleware',
        'django.middleware.common.CommonMiddleware',
        ...
    ]
    

    Here is the referenced lib. Django-CORS-Headers

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.