#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Defining JSON methods in Flask

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright so, this simple little bit of code is a pretty decent Flask application running in debug mode. What's going to be important for us is not
0:10 to return HTML, not really HTML, just text, but not really is it to return text and HTML for the browsers,
0:18 but is instead to return JSON to communicate. Remember, several times throughout this course, we've interacted with JSON APIs,
0:27 we had the Movie Search app, we had searching talk Python.fm, we've worked with GitHub, we've worked with other things,
0:34 and all that was consuming JSON APIs. Now we need to build one. So how does that work? Turns out to be not too hard here.
0:41 So let's come over here and this will just be api/test. And you got to make sure that you name this, let's just call this test api_test. Like that.
0:53 Now if we call this, it's just going to return Hello world. Or Hello Apytest. Let's just see that that actually is working.
1:01 And we're going to use Postman over here. So, if we go and just get a request, straight like that, we can look at the headers.
1:08 The content type is HTML and it's a 200. What if we go to api/test? Look we got a 200 again. That's really cool.
1:19 Right now we're getting HTML and the body is just this. So we have the routing working, and notice it automatically detected that change,
1:25 that's super cool. But, what we want to do is instead let's suppose we have some data. So this'll be name is Michael. Day, it's going to be 97.
1:39 What if we want to return this, like we could've gotten that from our database, for example, but we want to return this as JSON.
1:47 Super easy. So we're going to return flask.jsonify(data). Let's try that. Now if we do a get. Boom. There's our raw JSON coming back
2:00 and the type is application JSON. So that's all it takes really to build these APIs. Is we put in a route and then we have some data and we jsonify it.
2:10 The other aspect that we're going to need to work with is how do we work with whether this a HTTP GET or POST or PUT.
2:18 Because when you're working with the APIs remember, creational type things are done with POST and PUT. Read only stuff is done with GET, and so on.
2:25 So you'll see, we can go over here and say method=GET, or POST, or PUT, or both. We could actually handle GET and POST. Like that.
2:36 This one's only going to be for GET. Okay, so these are the building blocks that we need to build the seven operations that we've talked about.
2:44 The other stuff that we're going to need is the actual data, and the game play, and so on. And we've already written that
2:50 and we're just going to move that in, and copy some code over, and adapt it. Because we've already spent a lot of time working with that code.
2:56 And then we're going to adapt it to methods just like this.


Talk Python's Mastodon Michael Kennedy's Mastodon