Search code examples
pythonapiflaskserverjson-api

JSON API with Flask - 500 Internal Server Error


I'm an absolute newbie on Flask and API things. I want to host my Json with API in localhost with FLASK. But it says

500 Internal Server Error.

At my first try to run this Flask, it give me this 500 Internal Server error. Then I go from Ubuntu to Windows to play a game and browsing (I try to explain it clearly). And I come back again to Ubuntu.

And I'm doing the exactly same thing as I'm doing it in my first try which I got this 500 Internal Server error. BUT I DON'T KNOW, this time it works. And I'm sure that I don't do anything but just like my first try

This is the code

from flask import Flask
from flask import request
from pymongo import MongoClient
from bson.json_util import dumps
import json


client = MongoClient()
db = client.dbCrawlTwint

app = Flask(__name__)

@app.route("/get_statistic_year", methods = ['GET'])
def get_statistic_year():
        statisticYear = db.statisticYear.find()
        return dumps(statisticYear)

@app.route("/get_statistic_month", methods = ['GET'])
def get_statistic_month():
        statisticMonth = db.statisticMonth.find()
        return dumps(statisticMonth)

@app.route("/get_statistic_week", methods = ['GET'])
def get_statistic_week():
        statisticWeek = db.statisticWeek.find()
        return dumps(statisticWeek)

@app.route("/get_statistic_day", methods = ['GET'])
def get_statistic_day():
        statisticDay = db.statisticDay.find()
        return dumps(statisticDay)

And this is the error.log

INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:13] "GET /get_statistic_month HTTP/1.1" 500 -
ERROR:restAPI:Exception on /get_statistic_month [GET]
Traceback (most recent call last):
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/alramdein/Documents/Angular-exp/angular-statistic/flask/restAPI.py", line 21, in get_statistic_month
    return dumps(statisticMonth)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 383, in dumps
    return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in _json_convert
    return list((_json_convert(v, json_options) for v in obj))
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/bson/json_util.py", line 421, in <genexpr>
    return list((_json_convert(v, json_options) for v in obj))
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1225, in next
    if len(self.__data) or self._refresh():
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1140, in _refresh
    self.__send_message(q)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/cursor.py", line 1010, in __send_message
    helpers._check_command_response(first)
  File "/home/alramdein/anaconda3/envs/eventenv/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: there are no users authenticated
INFO:werkzeug:127.0.0.1 - - [01/Aug/2019 13:21:14] "GET /get_statistic_month HTTP/1.1" 500 -

And this is the errorAnd this is the error


Solution

  • I'm notice that there is a

    pymongo.errors.OperationFailure: there are no users authenticated

    When I post this question

    Just add this in Windows

    client = MongoClient('mongodb://user:pass@localhost:27017/')
    

    or this in Linux

    client = MongoClient('mongodb://user:[email protected]:27017/')
    

    I didn't delete this because other people might have the same problem with me