Having trouble with the 'POST' method: keep getting 405 "Method not allowed"


#1

I’m new to apistar and have been going through the documentation. When I try running the code here, at the Type System section in the documentation or the code here, in apistar/test/test_schema.py, I keep getting method not allowed.

I’m not entirely sure why this is happening and I have not been able to find many examples of the ‘POST’ method using apistar. Furthermore, when I do try to make that ‘POST’ request, the terminal seems to imply that it’s making a ‘GET’ request --> “GET /create_product/ HTTP/1.1” 405 -

To make the request, I am just going to the url 127.0.0.1/post_endpoint/ or 127.0.0.1/create_product/

Any input is appreciated!

Here are snippets of the code:

from apistar import App, Route, TestClient, validators, types, http
from apistar.server.handlers import serve_schema


class User(types.Type):
    name = validators.String(max_length=100)
    age = validators.Integer(allow_null=True, default=None)


def get_endpoint(name: str, age: int=None):
    """endpoint description"""
    raise NotImplementedError()


def post_endpoint(user: User, request: http.Request, query_params: http.QueryParams,
                  body: http.RequestData):
    
    raise NotImplementedError()


routes = [
    Route(url='/get-endpoint/', method='GET', handler=get_endpoint),
    Route(url='/post-endpoint/', method='POST', handler=post_endpoint),
    Route(url='/schema/', method='GET', handler=serve_schema, documented=False),
]
app = App(routes=routes)

test_client = TestClient(app)

if __name__ == '__main__':
    app.serve('127.0.0.1', 8080, debug=True)

And

from apistar import types, validators, App, Route, Include
from apistar.server.handlers import serve_static_wsgi

class Product(types.Type):
    name = validators.String(max_length=100)
    rating = validators.Integer(minimum=1, maximum=5)
    in_stock = validators.Boolean(default=False)
    size = validators.String(enum=['small', 'medium', 'large'])

data = {'name': 'shirt', 'rating': 4, 'size': 'large'}
product = Product(data)


def create_product(product: Product):
    # Save a new product record in the database.
    return "Done!"

routes = [
    Route('/create_product/', method='POST', handler=create_product),
    Route('/static', 'GET', handler=serve_static_wsgi)
]

app = App(routes=routes)

if __name__ == '__main__':
    app.serve('127.0.0.1', 5000, debug=True)

#2

Probably worth sharing an example of the code you’re using, and some more detail on how you’re making the request.


#3

My apologies, I’ve added some more details along with the code, please let me know if I can provide anything else and thank you!


#4

Can you confirm how you are trying to make the POST request? Are you visiting 127.0.0.1/post_endpoint/ in the browser or sending a request from the command line?

Be aware that if you are just visiting 127.0.0.1/post_endpoint/ in the browser then you will be making a GET request to that endpoint. Try to make a POST request from the command line with

curl -X POST 127.0.0.1/post_endpoint/

and let us know what the response is.


#5

Yes that was it! Boy do I feel foolish. The 405 is gone, now I just get a 400, but I have a feeling that could be due to some error in my code. Thank you for your help!