HTTP 406 Response Code Check is breaking my Google App Engine/Python API -
i have google app engine api using python , ndb working except http response code/error checking. put in code handle 406 (to accept json requests) , 400 errors (to prevent user leaving required field blank) post function 1 of entities seems have broken code. code error checking included:
class task_action(webapp2.requesthandler): def post(self): #only allows json, if not, error if 'application/json' not in self.request.accept: self.response.status = 406 self.response.status_message = "not acceptable, api supports application/json mime type" return new_task = task(parent=parent_key, name = self.request.get("task_name"), hours = int(self.request.get("task_hours")), id = self.request.get("task_name")) #has error code, since name , hours required if name: new_task.name = name else: self.response.status = 400 self.response.status_message = "invalid request, task name required." if hours: new_task.hours = hours else: self.response.status = 400 self.response.status_message = "invalid request, task hours required." key = new_task.put() out = new_task.to_dict() self.response.write(json.dumps(out))
i using curl test it:
curl --data-urlencode "name=clean" -h "accept: application/json" http://localhost:15080/task
i know problem in error checking code (all if else statements) because when take out curl test works fine , object added ndb database correctly. however, error checking code included curl test not add object should. have idea why error checking code breaking post statement? there better way return http error response codes?
you had uninitialized variables in code (name
, hours
, maybe parent_key
) , didn't return after preparing error response, flowing in areas code wouldn't work.
i'd suggest re-organizing error checking code minimal impact on functional code (checks should done possible, simplify remaining functional code. also, prefer use more concise webapp2.abort()
function (which doesn't need return
statement).
something along these lines:
class task_action(webapp2.requesthandler): def post(self): # allows json, if not, error if 'application/json' not in self.request.accept: webapp2.abort(406, details="not acceptable, api supports application/json mime type") # request must contain valid task name name = self.request.get("task_name") if not name: webapp2.abort(400, details="invalid request, task name required.") # request must contain valid task hours try: hours = int(self.request.get("task_hours")) except exception: hours = 0 if not hours: webapp2.abort(400, details="invalid request, task hours required.") new_task = task(parent=parent_key, name=name, hours=hours, id=hours) new_task.name = name # isn't done task() above? new_task.hours = hours # isn't done task() above? key = new_task.put() out = new_task.to_dict() self.response.write(json.dumps(out))
another note: you're specifying id
parameter in task()
call, doesn't work unless know each task() entity has unique hours
value. may want let datastore assign ids automatically.
Comments
Post a Comment