Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask REST API Resourceful Blueprints Marshalling

Empty Array or List

Hello, at around the 6-minute mark of the video, after he is done implementing marshal into the get method, Postman keeps returning an empty array or list. I've been over the code several times and can spot no discrepancies between Kenneth's and mine. Has Flask gone through an update that had rendered the code in the video in error? Thanks!

Megan Amendola
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Megan Amendola
Treehouse Teacher

Hi! Flask has gone through a few updates since that course was created. If you post your code, it will be easier to see if there is in fact any difference or missing elements that might be throwing your code off.

1 Answer

Hello, I have the same issue. I use the same code in the workspace, the only exception is that I use flast_restful instead of flask.ext etc. from flask_restful import (Resource, Api, reqparse, inputs, url_for, fields, marshal, marshal_with)

The post gives me a 404 error, and the get does not give me what I expexct. I will really appreciate the help, thanks. Also, I dont know how to post code that well, but is really the same code as the last workspace, thanks!

from flask import jsonify, Blueprint, abort

from flask_restful import (Resource, Api, reqparse, url_for,
                           inputs, fields, marshal, marshal_with)

import models

course_fields = {
    'id': fields.Integer,
    'title': fields.String,
    'url': fields.String,
    'reviews': fields.List(fields.String)
}

def add_reviews(course):
    course.reviews = [url_for('resources.reviews.review', id=review.id)
                      for review in course.review_set]
    return course

def course_or_404(course_id):
    try:
        course = models.Course.get(models.Course.id==course_id)
    except models.Course.DoesNotExist:
        abort(404)
    else:
        return course

class CourseList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'title',
            required=True,
            help='No course title provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'url',
            required=True,
            help='No course URL provided',
            location=['form', 'json'],
            type=inputs.url
        )
        super().__init__()

    def get(self):
        courses = [marshal(add_reviews(course), course_fields)
                   for course in models.Course.select()]
        return {'courses': courses}

    @marshal_with(course_fields)
    def post(self):
        args = self.reqparse.parse_args()
        course = models.Course.create(**args)
        return (add_reviews(course), 201,
                {'Location': url_for('resources.courses.course', id=course.id)})

class Course(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'title',
            required=True,
            help='No course title provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'url',
            required=True,
            help='No course URL provided',
            location=['form', 'json'],
            type=inputs.url
        )
        super().__init__()

    @marshal_with(course_fields)
    def get(self, id):
        return add_reviews(course_or_404(id))

    @marshal_with(course_fields)
    def put(self, id):
        args = self.reqparse.parse_args()
        course = course_or_404(id)
        query = course.update(**args)
        query.execute()
        course = course_or_404(id)
        return (course, 200,
                {'Location': url_for('resources.courses.course', id=id)})

    def delete(self, id):
        course = course_or_404(id)
        query = course.delete()
        query.execute()
        return '', 204, {'Location': url_for('resources.courses.courses')}

courses_api = Blueprint('resources.courses', __name__)
api = Api(courses_api)
api.add_resource(
    CourseList,
    '/api/v1/courses',
    endpoint='courses'
)
api.add_resource(
    Course,
    '/api/v1/courses/<int:id>',
    endpoint='course'
)

[Code markdown edited by staff] - Make sure to use backticks ` :)