Python, Bottle, MongoDB Create Function 500 Error Code

I am creating a REST service using Bottle and MongoDB. The problem is I am getting a 500 error code when trying to insert a document.

            <title>Error: 500 Internal Server Error</title>
            <style type="text/css">
              html {background-color: #eee; font-family: sans;}
              body {background-color: #fff; border: 1px solid #ddd;
                    padding: 15px; margin: 15px;}
              pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
            <h1>Error: 500 Internal Server Error</h1>
            <p>Sorry, the requested URL <tt>&#039;http://localhost:8080/create&#039;</tt>
               caused an error:</p>
            <pre>Unhandled exception</pre>


curl -H "Content-Type: application/json" -X POST -d '{"id" : "10011-2017-TEST","certificate_number" : 9278833,"business_name" : "ACME TEST INC.","date" : "Feb 20 2017","result" : "No Violation Issued","sector" : "Test Retail Dealer - 101"}' http://localhost:8080/create

The thing is though, I am able to successfully insert a document even though I get the 500 error code. Why am I getting the 500 error code? I have been trying to implement exception handling for both MongoDB and Bottle. Not sure if that is why I am getting the code. If not, how can I properly implement exception handling? I have read up on abort but I have had trouble with that. Also, when I comment out return result , I get a 200 response code, but I want to return the id of the document inserted. Thanks

Source Code:

# -*- coding: utf-8 -*-
from bson import json_util
import json
import bottle
from bottle import route, run, request, abort, post, error
import pymongo
from pymongo import MongoClient
import datetime as datetime

connection = MongoClient('localhost', 27017)
db = connection['city']
collection = db['inspections']

@route('/create', method='POST')
def insert_document():

        data = request.json
        result = collection.insert_one(data).inserted_id
    except Exception, e:

        print ('EXCEPTION: ', e)

    return result

if __name__ == '__main__':

    run(host='localhost', port=8080)

1 answer

  • answered 2020-02-13 01:12 Belly Buster


        return result


        return str(result)

    or simply remove the line altogether.