FIguring out Flask

I’ve been figuring out blockchains thanks to the post on Medium Learn Blockchains by Building One, which overall is pretty good but there were some issues that cropped up that didn’t quite make sense. First I couldn’t get the routes to work for some reason, specifically to the /mine endpoint because I kept getting a 404 error and it wouldn’t show up. I kept going back to the article and even went to the source code but couldn’t figure it out. Even Postman/Curl wasn’t any help because every time I tried to access the API it gave me the error of “Unexpected <” which even after some googling gave me no luck.

I ended up finding out what it was by googling this morning that my Flask routes weren’t working, as it turns out I was putting the routes in the wrong place. After googling “Flask route not working”, I found This Stackoverflow post that explained pretty clearly what was happening, which was that the app.run() was running before the new routes were declared, meaning that the server started up and only saw the routes declared above it not below. For example:


# Incorrect!!!

if __name__ == '__main__':
    app.run(host = '127.0.0.1', port = 5000)

@app.route('/hello', methods = ['GET'])
def hello():
	return "hello world"


Versus:


# Correct
@app.route('/hello', methods = ['GET'])
def hello():
	return "hello world"

if __name__ == '__main__':
    app.run(host = '127.0.0.1', port = 5000)


Long story short: order matters.

The blockchains post has also helped teach me about APIs and about Postman (which I really couldn’t figure out before) and how/why they work which has been really nice.

Written on October 8, 2017