0

I am trying to deploy restful API on google cloud. The code is working fine on my local. However, after the successful deployment of my application on google cloud when I am hitting the project URL, I am getting the following error:

Can't read from server. It may not have the appropriate access-control-origin settings.

I searched and found this thread similar to my issue. I followed it and created CORS for my project. Following is the created CORS on the project: [Ran - gsutil cors get gs://project-name]

[{"maxAgeSeconds": 86400, "method": ["GET", "POST", "OPTIONS"], "origin": ["*"], "responseHeader": ["Origin", "Accept", "X-Requested-With", "Authorization", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token"]}]

Following is the service:

from services import api
from services import Resource
from services import fields

import models

api = api.namespace(name='College', description='RESTFul API for College')

college = api.model('College', {'name' : fields.String('name'),
                                'short_name' : fields.String('short_name')})

@api.route('/college')
class CollegeList(Resource):
    @api.marshal_with(college)
    def get(self):
        return models.College.query.all()

    @api.expect(college)
    def post(self):
        name = api.payload.get('name')
        short_name = api.payload.get('short_name')
        college = models.College(name=name, short_name=short_name)
        models.db.session.add(college)
        models.db.session.commit()
        return {'result' : name+' is Added!'}, 201

    @api.route('/college/<int:id>')
    class College(Resource):
        @api.marshal_with(college)
        def get(self, id):
            return models.College.query.filter(models.College.id == id).one()

Is there anything needed in app.yaml to configure CORS?

miserable
  • 697
  • 1
  • 12
  • 31

1 Answers1

0

CORS support is required when you intend to access assets hosted on a server (let's call it Server B) different than the one where you deploy your app (Server A). You will need to allow HTTP requests coming from a different domain on the side of your Server B (the one that hosts the data you're accessing from your app). In order to do that, if Server B is hosted in App Engine, specify the Access-Control-Allow-Origin http header in the app.yaml file, as it is explained in Google docs:

handlers:
- url: /some_url
  ...
  http_headers:
    Access-Control-Allow-Origin: http://server-a.appspot.com

You can also use the wildcard '*' in place of the server-a URL to allow access from any domain.

arudzinska
  • 3,152
  • 1
  • 16
  • 29