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