Mathew
Mathew

Reputation: 1460

adding http response header

I have written a web app in Heroku in python and would like to grant javascript clients basic access to my resources. I have been reading this article on how to do that: https://www.w3.org/wiki/CORS_Enabled

and from the article I have found that I should do the following:

print("Content-Type: text/turtle")
print("Content-Location: mydata.ttl")
print("Access-Control-Allow-Origin: *")

procfile is as follows: web: python app.py
and app.py is as follows:

#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
@bottle.route('/')

def index():
    print("Content-Type: text/turtle")
    print("Content-Location: mydata.ttl")
    print("Access-Control-Allow-Origin: *")
    return("testing")

bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))

however I still can't access the resources, I am getting this error:

Failed to load https://ksmlgames.herokuapp.com/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

thanks for the help

#!/usr/bin/env python
import gevent.monkey
gevent.monkey.patch_all()
import bottle
import os
@bottle.route('/')


def index():
    response.set_header("Content-Type", "text/turtle")
    response.set_header("Content-Location", "mydata.ttl")
    response.set_header("Access-Control-Allow-Origin", "*")
    return("testing")

bottle.run(server='gevent', host='0.0.0.0', port=os.environ.get('PORT', 5000))

@thmsdnnr, This seems to be working, thanks

Upvotes: 0

Views: 83

Answers (1)

thmsdnnr
thmsdnnr

Reputation: 1302

You'll want to use response.set_header.

def index():
  response.set_header("Content-Type", "text/turtle")
  response.set_header("Content-Location", "mydata.ttl")
  response.set_header("Access-Control-Allow-Origin", "*")
  return("testing")

If you find yourself doing this for many routes, you can set up an 'after_request' hook like this.

Upvotes: 1

Related Questions