python - How allow HTTP methods "PUT" and "DELETE" in Flask? -


i'm beginning in python , try put , delete methods this:

import gamerocket flask import flask, request, render_template app = flask(__name__)  gamerocket.configuration.configure(gamerocket.environment.development,                                 apikey = "my_apikey",                                 secretkey = "my_secretkey")  @app.route("/update_player", methods = ["put"]) def update_player():     result = gamerocket.player.update(         "a_player_id",         {             "name" : "bob_update",             "emailhash" : "update@test.com",             "totalpointsachievement" : 1         }     )  if result.is_success:     return "<h1>success! " + result.player.id + " " + result.player.name +   "</h1>" else:     return "<h1>error " + result.error + ": " + result.error_description  if __name__ == '__main__': app.run(debug=true) 

but http 405 error method not allowed

the method not allowed requested url.

could me?

edit: here how call method:

class playergateway(object):     def update(self, player_id, params={}):     response = self.config.http().put("/players/" + player_id, params)     if "player" in response:         return successfulresult({"player": player(self.gateway,response["player"])})     elif "error" in response:         return errorresult(response)     else:         pass 

next in http:

class http(object):     def put(self, path, params={}):         return self.__http_do("put", path, params)     def delete(self, path, params={}):         return self.__http_do("delete", path, params)      def __http_do(self, http_verb, path, params):          http_strategy = self.config.http_strategy()          full_path = self.environment.base_url + "/api/" + self.config.api_version() + path         params['signature'] = self.config.crypto().sign(http_verb, full_path, params,                               self.config.secretkey)          params = self.config.sort_dict(params)          request_body = urlencode(params) if params !={} else ''          if http_verb == "get":             full_path += "?" + request_body         elif http_verb == "delete":             full_path += "?" + request_body          status, response_body = http_strategy.http_do(http_verb, full_path,                              self.__headers(),request_body)          if http.is_error_status(status):             http.raise_exception_from_status(status)         else:             if len(response_body.strip()) == 0:                 return {}             else:                 return json.loads(response_body)      def __headers(self):         return {             "accept" : "application/json",             "content-type" : "application/x-www-form-urlencoded",             "user-agent" : "gamerocket python " + version.version,             "x-apiversion" : gamerocket.configuration.configuration.api_version()         } 

and determine request strategy:

import requests  class requestsstrategy(object):     def __init__(self, config, environment):         self.config = config         self.environment = environment      def http_do(self, http_verb, path, headers, request_body):          response = self.__request_function(http_verb)(             path,             headers = headers,             data = request_body,             verify = self.environment.ssl_certificate,         )          return [response.status_code, response.text]      def __request_function(self, method):         if method == "get":             return requests.get         elif method == "post":             return requests.post         elif method == "put":             return requests.put         elif method == "delete":             return requests.delete 

i have not attempted run code believe can see going on.

class playergateway(object):     def update(self, player_id, params={}):     response = self.config.http().put("/players/" + player_id, params)     if "player" in response:         return successfulresult({"player": player(self.gateway,response["player"])})     elif "error" in response:         return errorresult(response)     else:         pass 

you calling route base_url/api/version/players/ calling code.

however registering route "/update_player" put method

without seeing rest of flask app, cannot tell if problem, have define methods allowed each root. :)


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -