From the course: Building RESTful APIs with Flask
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Returning JSON instead of text
From the course: Building RESTful APIs with Flask
Returning JSON instead of text
- [Instructor] So far we have two really simple endpoints, both of which are returning plain text. While there's nothing inherently wrong with this, restful APIs are generally expected to return JSON, or JavaScript Object Notation, rather than just a string. So let's modify our code so that instead of returning text, we're returning valid JSON. Let's go into our code, and we're going to modify our super simple route so that it returns JSON. All I'm going to do is add just one little function call, and it's called jsonify. And what I'll do here, is I'll set message equal to, and then I'll just keep the text that's there. Now the jsonify function has a red line under it because although it's part of Flask, I didn't actually import it explicitly. So let's go to the very top of our file, and lets add another import from Flask. So we're going to import Flask and we're going to import jsonify. Now if I scroll back down I'll see that that red line is gone and we're ready to test this. I'll…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.