Python – Application Engine : “URLFetch is not available in this environment.”

Application Engine : “URLFetch is not available in this environment.”… here is a solution to the problem.

Application Engine : “URLFetch is not available in this environment.”

I built an App Engine API in Python, which was fetched by the Node application. The API works as expected (1) getting and publishing requests in production and (2) getting requests in development. It fails on a publish request in development, I could use some help to figure out why.

Error message

In my node environment, I see the error:

No ‘Access-Control-Allow-Origin’ header is present on the requested
resource. Origin ‘http://localhost:4444 ‘ is therefore not allowed
access. The response had HTTP status code 500. If an opaque response
serves your needs, set the request’s mode to ‘no-cors’ to fetch the
resource with CORS disabled.

But I’ve used flask_cors packages in my app, so I’m wondering if this is really a CORS issue.

Virtual python environment log I activated:

File
“/myproject/googleAdsApi/env/lib/python2.7/site-packages/urllib3/contrib/appengine.py”,
line 103, in init
“URLFetch is not available in this environment.”)

So maybe I should use an alternative to URLFetch in my virtual environment?

My current implementation

Get:

fetch('http://localhost:8080/api/get_accounts', {
    method: "POST",
    mode: "cors", 
    cache: "no-cache", 
    credentials: "same-origin", 
    headers: {
        "Content-Type": "application/json; charset=utf-8",
    },
    redirect: "follow", 
    referrer: "no-referrer", 
    body: JSON.stringify(credentials)
})
.then(response => response.json())
.then(result => console.log(result));

flask_cors:

app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

Solution

Always use dev_appserver.py to run a local development environment on a GAE application. GAE has many features that are difficult to reproduce manually on local virtualenv. In addition, you get many useful tools to monitor various services (task queues, memory caches, storage, etc.). dev_appserver.py also automatically loads a large number of GAE native APIs for you to use, and they often have their own versions of popular libraries for serverless environments (URLFetch is one of them

).

Official documentation
https://cloud.google.com/appengine/docs/standard/python/tools/using-local-server

Related Problems and Solutions