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

Daniel Bowen
Daniel Bowen
8,577 Points

When using postman, my post method is ignored (Flask API)

Whenever I do a post request to http://0.0.0.0:8000/api/v1/fliers it ignores it. I get a 200 OK status back and the database remains unchanged. My get request to the same comes back as its supposed to, but the post request is ignored.

Weird thing is everything was working fine just a bit ago, and now it isn't. I'm thoroughly confused. Any help would be appreciated.

from flask import jsonify, Blueprint, abort

from flask.ext.restful import (Resource, Api, reqparse,
                               inputs, fields, marshal,
                               marshal_with, url_for)

from auth import auth
import models

flier_fields = {
    'name': fields.String
}

def flier_or_404(flier_id):
    try:
        flier = models.Flier.get(models.Flier.id==flier_id)
    except models.Flier.DoesNotExist:
        abort(404)
    else:
        return flier


class FlierList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'name',
            required=True,
            help='No name provided',
            location=['form', 'json']
        )
        super(FlierList, self).__init__()

    def get(self):
        fliers = [marshal(flier, flier_fields) for flier in models.Flier.select()]
        return {'fliers': fliers}

    @marshal_with(flier_fields)
    @auth.login_required
    def post(self):
        args = self.reqparse.parse_args()
        flier = models.Flier.create(**args)
        return (flier, 201)

class Flier(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'name',
            required=True,
            help='No name provided',
            location=['form', 'json']
        )
        super(Flier, self).__init__()

    @marshal_with(flier_fields)
    def get(self, id):
        return flier_or_404(id)

    def put(self, id):
        return 400

    @auth.login_required
    def delete(self, id):
        query = models.Flier.delete().where(models.Flier.id==id)
        query.execute()
        return ('', 204)

fliers_api = Blueprint('resources.fliers', __name__)
api = Api(fliers_api)
api.add_resource(
    FlierList,
    '/api/v1/fliers',
    endpoint='fliers'
)
api.add_resource(
    Flier,
    '/api/v1/fliers/<int:id>',
    endpoint='flier'
)