I am building a Webservices API using Flask-Restless, Flask-SQLAlchemy and SQLite3. I have a class called Place, which also is a table containing different places around my city. From what I understand I should be able to post new data at endpoints like so:
http://localhost:5000/webservices/api/place
I also should be able to see a json response of everything in that table when I navigate to that url in a browser. This only works part of the time... some times I am able to see the json response, and other times I get a 404 url not found error. I keep refreshing the page and eventually the json response comes back. I still am not able to post the api endpoint without getting a 404 (or a 500 occasionally?).... am I doing something wrong?
I am posting like this:
>>> import json
>>> import requests .
>>> newplace = {'name': u'Carls Jr.', 'address': '123 Main Street'}
>>> r = requests.post('http://localhost:5000/webservices/api/place', data=json.dumps(newplace),
... headers={'content-type': 'application/json'})
>>> r.status_code, r.headers['content-type']
(404, 'text/html')
Here's my code:
import flask
import flask.ext.sqlalchemy
import flask.ext.restless
# Create the Flask application and the Flask-SQLAlchemy object.
app = flask.Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = flask.ext.sqlalchemy.SQLAlchemy(app)
#place
class Place(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode, unique=True)
address = db.Column(db.Unicode)
# Create the database tables.
db.create_all()
# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Place, methods=['GET', 'POST', 'DELETE'])
# start the flask loop
if __name__ == '__main__':
app.run()