【问题标题】:CherryPy receives empty POST from emberjs RESTAdapterCherryPy 从 emberjs RESTAdapter 接收空 POST
【发布时间】:2023-04-02 14:30:01
【问题描述】:

我正在探索使用 CherryPy 作为后端和 emberjs 作为前端来管理书籍列表的简单 Web 应用程序。 Cherrypy 只是根据索引请求提供车把模板:

import os
import cherrypy
from google.appengine.api import users
from google.appengine.ext import ndb

class Root:

    def __init__(self):
        # book REST API
        self.books = BookAPI()

    @cherrypy.expose
    def index(self):
        return open(os.path.join(template_env, 'index.hbs'))

我使用 BooksAPI 和 Books 类作为 RESTfull API,使用 Google 数据存储来存储图书对象(我现在只存储 isbn)。

class BookAPI():
    exposed=True

    @cherrypy.tools.json_out()
    def GET(self, isbn=None):

        # get the current user
        user = users.get_current_user()

        if(isbn is None):

            # query all books current user
            ancestor_key = ndb.Key("Library", str(user.user_id()))
            books = Book.query_books(ancestor_key).fetch(20)

            # convert to JSON list of books
            book_list = []
            for index, b in enumerate(books):
                book_list.append({'id': index, 'isbn': b.isbn})
            result = {
                "books": book_list
            }

        return result

    def POST(self, isbn):

        # get the current user
        user = users.get_current_user()

        # create book and save in data storage
        parent_key = ndb.Key('Library', user.user_id())
        book = Book(parent=parent_key, isbn=isbn)
        book.put()

    ...

class Book(ndb.Model):

    isbn = ndb.StringProperty()

    @classmethod
    def query_books(cls, ancestor_key):
        return cls.query(ancestor=ancestor_key)

对于 emberjs 客户端,我使用 RESTAdapter:

window.Books = Ember.Application.create();
Books.ApplicationAdapter = DS.RESTAdapter.extend();

我的emberjs书模型定义如下:

Books.Book = DS.Model.extend({
  isbn: DS.attr('string'),
});

我添加了以下图书控制器:

Books.BookController = Ember.ObjectController.extend({
  actions: {
    removeBook: function() {
      var book = this.get('model');
      book.deleteRecord();
      book.save();
    }
  }
});

Books.BooksController = Ember.ArrayController.extend({
  actions: {
    createBook: function() {
      // get book isbn
      var isbn = this.get('newIsbn');
      if(!isbn.trim()) { return; }
      // create new book model
      var book = this.store.createRecord('book', {
        isbn: isbn,
      });
      // clear the 'new book' text field
      this.set('newIsbn', '');
      // Save model
      book.save();
    }
  }
});

最后是以下路线:

Books.Router.map(function () {
  this.resource('books', { path: '/' });
});

Books.BooksRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('book');
  }
});

使用 FixedAdapter 添加和删除书籍有效,然后我切换到 RESTAdapter。

GET 方法有效。 Emberjs 自动调用 GET 方法,成功获取到 index.hbs 模板中展示的 JSON 格式书籍列表。

但是,emberjs 以我没想到的方式调用 POST 方法。 ember 似乎发送了一个空的 POST,而没有将 isbn 添加为 POST 数据。因为当我从cherrypy POST 函数中删除isbn 关键字参数时,该函数会被调用。不过,我需要 isbn 来创建 book 对象。

我可能在这里忘记了一些明显的东西,但我不知道是什么。有谁知道我忘记了什么或做错了什么?谢谢。

巴斯蒂安

【问题讨论】:

  • 您在 ember 中的模型定义如何?
  • 我更新了问题,添加了ember book模型。这是我定义的唯一模型。

标签: ember.js cherrypy


【解决方案1】:

为了保存新记录,Ember 发送一个 json 表示正在保存在帖子正文中的对象...

你的情况应该是

book:{isbn:[the isbn value]}

所以没有isbn参数

你能在你的帖子功能上测试一下吗

def POST(self):

    # get the current user
    user = users.get_current_user()
    cl = cherrypy.request.headers['Content-Length']
    rawbody = cherrypy.request.body.read(int(cl))
    body = simplejson.loads(rawbody)
    bookFromPost = body.book
    # create book and save in data storage
    parent_key = ndb.Key('Library', user.user_id())
    book = Book(parent=parent_key, isbn=bookFromPost.isbn)
    book.put()

您应该返回一个 201 创建的 HTTP 代码,其中包含指定 id 的图书的 json 表示

book:{id:[the new id],isbn:[the isbn value]}

【讨论】:

  • 太棒了,做了两个小改动就可以了。 body 和 bookFromPost 都是字典,因此我需要使用 body['book'] 代替 body.book 和 bookFromPost['isbn'] 代替 bookFromPost.isbn。谢谢,这一切都清楚了。
  • 对不起,我没有要测试的环境,python 对我来说不是很熟悉 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2015-05-24
  • 2011-04-14
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多