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?

  • when we run the hello-world image its throws the connection refused error message
  • Fail to expose my pod to the internet
  • docker AWS fail to launch - custom kernel
  • How to see image tag name in my private docker registry
  • In docker run, how to set multi-value in --ulimit
  • Can't get remote Hazelcast client to connect to cluster running in docker containers
  • 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.

  • How to setup sockets on docker-compose version 2?
  • how process static file when deploying django using docker and aws eb
  • Connect docker container to both internet and VPN
  • How to add tags to docker images made using autobuild
  • Building a Docker image with deprecated dependencies
  • Gitlab Continuous Integration on Docker
  • 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.