Spyne:如何使用HttpRpc将POST解码为JSON

时间:2014-06-11 22:31:33

标签: json rest spyne

我正在尝试使用sypne构建REST应用程序。我想收到一个JSON文档(但没有使用被称为对象名称的方法。我举例说明: 我使用以下内容对网址http://localhost/root发帖:

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

我有我的间谍申请:

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()
      )

知道必须有更好的方法来做到这一点!任何提前获得文档或解决方案的指示!

此致

1 个答案:

答案 0 :(得分: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}

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

致以最诚挚的问候,

相关问题