1

Trying to use Postman to test and see if my post method of an API I'm building works. I keep getting a 405 error, suggesting to me that the functionality of posting isn't even available. But it's a pretty straightforward class, so I can't see what's wrong.

from flask.views import MethodView
from flask import jsonify, request, abort

class BookAPI(MethodView):

    books = [
        {"id":1, "title":"Moby Dick"},
        {"id":2, "title":"Grapes of Wrath"},
        {"id":3, "title":"Pride and Prejudice"}
    ]

    def get(self):
        return jsonify({"books": self.books})

    def post(self):
        if not request.json or not 'title' in request.json:
            abort(400)

        book = {
        "id": len(self.books) + 1,
        "title": request.json['title']
        }

        self.books.append(book)
        return jsonify({'book':book}), 201

The get method is working fine. I can see it on my localhost. But when I try to post to my localhost with postman - 405 error

This is all I'm posting to http://localhost/books/

{
   "title": "Frankenstein"
}
Hofbr
  • 868
  • 9
  • 31
  • Can you edit your code to be runable including route regsistration and how you run the app? And maybe a curl example we can use to test. I think you can export the Postman request as curl. – Mihai Apr 06 '21 at 18:45
  • It works as expected when I tried to run it locally you see this [screenshot](https://prnt.sc/115xhar) and code [here](https://codeshare.io/5ww6nP) btw I have used insomnia as client as I didn't had postman. – pb36 Apr 06 '21 at 18:46
  • I'm running the app with my terminal using docker. Specifically I used the command docker-compose run --service-ports web. I have it in a github repo if you'd like to take a crack running it: https://github.com/branhoff/wishlist-api. – Hofbr Apr 06 '21 at 18:47

1 Answers1

1

Thanks for the additional details, I tried running your application and it does work for me, the only change I made is the actual URL. You have registered your books api under the /books prefix. This is the specific place in your code https://github.com/branhoff/wishlist-api/blob/ee9fc696069d98513a89c249d23874d429684d54/book/views.py#L7

curl -X POST -H "Content-Type: application/json" -d '{"title": "GoT"}' http://localhost:80/books/

enter image description here

Mihai
  • 831
  • 6
  • 13
  • Thanks for confirming it worked for you. I killed my docker container and rebuilt it and it now it works. Perhaps I'm confused. I thought as I made changes, they would be reflected automatically. Don't suppose you know where my assumptions are going off the rails? Do I need to commit my changes to docker and then re-run? – Hofbr Apr 06 '21 at 19:15
  • 1
    I don't think code auto reload does not work with your setup as it's closer to the production way of running Flask apps where you don't code changes to be picked up by accident. This thread answers the auto reload question: https://stackoverflow.com/questions/16344756/auto-reloading-python-flask-app-upon-code-changes – Mihai Apr 06 '21 at 19:30