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 API Protection User Resource

Ben Moore
Ben Moore
22,588 Points

405 Method Not Allowed - Post User

I cannot post a user. I am running Pycharm and Python 3.6 as I have for each of the Python lessons I've taken thus far.

I can get reviews and courses no problem. However, I cannot post user. I have even copied the code from the course console.

Am I missing something or has the syntax changed?

users.py

import json
from flask import jsonify, Blueprint, abort, make_response

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

import models

user_fields = {
    'username': fields.String,
}


class UserList(Resource):
    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument(
            'username',
            required=True,
            help='No username provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'email',
            required=True,
            help='No email provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'password',
            required=True,
            help='No password provided',
            location=['form', 'json']
        )
        self.reqparse.add_argument(
            'verify_password',
            required=True,
            help='No password verification provided',
            location=['form', 'json']
        )
        super().__init__()

        def post(self):
            args = self.reqparse.parse_args()
            if args['password'] == args['verify_password']:
                user = models.User.create_user(**args)
                return marshal(user, user_fields), 201
            return make_response(
                json.dumps({
                    'error': 'Password and password verification do not match'
                }), 400)


users_api = Blueprint('resources.users', __name__)
api = Api(users_api)
api.add_resource(
    UserList,
    '/users',
    endpoint='users'
)
Ben Moore
Ben Moore
22,588 Points

Just realized my mistake. The post method was indented to sit under the init method. Simple fix but heck of a head-scratcher.