0

我正在尝试使用 sypne 构建一个 REST 应用程序。我想接收一个 JSON 文档(但不是使用称为对象名称的方法。我举个例子来澄清一下:我http://localhost/root用以下内容对 URL 进行 POST:

{ "id": 1, "desc": "number" }

我有我的 spyne 应用程序:

class HelloWorldService(ServiceBase):
    @rpc(_returns=AnyDict)
    def root(ctx):
        json_in=""
        for data in ctx.in_string:
            json_in = json_in + data
        return json.loads(json_in)

application = Application([HelloWorldService],
          tns='com.hello.webservices',
          in_protocol=HttpRpc(validator='soft'),
          out_protocol=JsonDocument()
      )

而且我知道必须有更好的方法来做到这一点!提前感谢任何指向文档或解决方案的指针!

问候

4

1 回答 1

0

Spyne 没有开箱即用的功能,您需要为此实现自己的协议。

不过这很容易。在我的头顶上:

class HybridHttpJsonDocument(JsonDocument):
    def create_in_document(self, ctx):
        super(HybridHttpJsonDocument, self).create_in_document(ctx)

        # get url fragment from ctx.transport.req_env

        ctx.in_document = {url_fragment: ctx.in_document}

这就是我能想到的你需要做的。请报告您的结果!

最好的祝福,

于 2014-06-12T12:25:36.547 回答