I’m using jQuery’s AJAX function, like so:
$.ajax ({ "url" : "/note",
"async" : true,
"type" : "POST",
"data" : {"id" : id, "content" : el.value}
});
On the backend (Python Google App Engine, currently using webapp.RequestHandler), PUT requests are handled by the same function that handles POST requests, like so:
class uNote(webapp.RequestHandler):
def post(self):
user = users.get_current_user()
if user:
logging.critical("ID: " + self.request.get('id'))
note = stickynote.db.get( self.request.get('id') )
if note:
content = self.request.get( 'content' )
if content:
note.content = content
else:
note.x = int(self.request.get('x'))
note.y = int(self.request.get('y'))
note.z = int(self.request.get('z'))
note.put()
self.response.out.write ( "true" );
else:
self.response.out.write ("no id found")
def put(self):
user = users.get_current_user()
if user:
logging.critical("ID: " + self.request.get('id'))
note = stickynote.db.get( self.request.get('id') )
if note:
content = self.request.get( 'content' )
if content:
note.content = content
else:
note.x = int(self.request.get('x'))
note.y = int(self.request.get('y'))
note.z = int(self.request.get('z'))
note.put()
self.response.out.write ( "true" );
else:
self.response.out.write ("no id found")
If I make a POST request everything works. If I change it to a PUT request (by changing type of the AJAX call), nothing works. If I check the logs for GAE, I see:
BadKeyError: Invalid string key .
This is the result of me logging the ID for each call:
ID: ahFkZXZ-YXp2ZC13ZWJub3Rlc3ItCxIKU3RpY2t5Tm90ZSIQdGVzdEBleGFtcGxlLmNvbQwLEgdzbk1vZGVsGAIM (from the POST call)
ID: (from the PUT call)
So clearly it’s not passing the ID properly for PUT calls, which is why the datastore is giving me a BadKeyError. I’m not sure if this is a problem on jQuery’s side, or if it’s from GAE’s side, though, and I have no idea what’s going on. Any help would be appreciated.
self.request.POST, and henceself.request.get()don’t return form data for PUT requests (or any non-POST requests). While it’s not usual to submit form data using a PUT request (more commonly, you’re submitting something as the body of the request), it’s certainly a little odd that they’re deliberately disabling it here.If you want to access the data, you’ll need to decode it from the body yourself, either using
urlparse.parse_qs(self.request.body), or like this.Another option would be to modify your API so you’re not using form data – for instance, by making the ID part of the URL, and send the content as the raw body of the PUT request.