1

I am trying to insert values in mongodb using python api. I have tried the following:

from flask import Flask
from flask import jsonify
from flask import request
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'star_db'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/star_db'

mongo = PyMongo(app)
@app.route('todo/api/star', methods=['POST'])
def add_star():
  star = mongo.db.stars
  name = request.json['name']
  distance = request.json['distance']
  star_id = star.insert({'name': name, 'distance': distance})
  new_star = star.find_one({'_id': star_id })
  output = {'name' : new_star['name'], 'distance' : new_star['distance']}
  return jsonify({'result' : output})

if __name__ == '__main__':
    app.run(debug=True)

and I am getting the error that 'The method is not allowed for the requested URL.'

I a doubtfull about the route I am giving here but not sure.

MSDN.WhiteKnight
  • 664
  • 7
  • 30
cherry
  • 11
  • 2

1 Answers1

0

Send correct POST requests from Frontend or add your method (e.g.GET) to the list of methods.

@app.route('todo/api/star', methods=['GET', 'POST'])
wowkin2
  • 5,895
  • 5
  • 23
  • 66