Python 为什么json请求的响应是空的?
我有一个工作应用程序,它接受SOAP请求,处理请求,将SOAP请求转发给API,处理响应,然后将响应转发给客户端 我正在尝试更改此应用程序,以便在我的应用程序和客户端之间使用JSON,但在API和应用程序之间仍然使用SOAPPython 为什么json请求的响应是空的?,python,json,django,rpc,spyne,Python,Json,Django,Rpc,Spyne,我有一个工作应用程序,它接受SOAP请求,处理请求,将SOAP请求转发给API,处理响应,然后将响应转发给客户端 我正在尝试更改此应用程序,以便在我的应用程序和客户端之间使用JSON,但在API和应用程序之间仍然使用SOAP class MyDjangoApplication(DjangoApplication): def __call__(self, request, *args, **kwargs): retval = self.HttpResponseObject() d
class MyDjangoApplication(DjangoApplication):
def __call__(self, request, *args, **kwargs):
retval = self.HttpResponseObject()
def start_response(status, headers):
# Status is one of spyne.const.http
status, reason = status.split(' ', 1)
retval.status_code = int(status)
for header, value in headers:
retval[header] = value
environ = request.META.copy()
if request.method == 'POST':
response = self.handle_rpc(environ, start_response)
else:
home_path = reverse('proxy:list_method')
uri = MY_ENDPOINT_URL or request.build_absolute_uri(home_path)
# to generate wsdl content
response = self._WsgiApplication__handle_wsdl_request(environ, start_response, uri)
if request.path == home_path and _is_wsdl_request(environ):
fn = None
elif 'method_name' in kwargs:
fn = view_method
else:
fn = list_method
if fn:
return fn(request, app=self, *args, **kwargs)
self.set_response(retval, response)
return retval
现在,它可以成功地接受来自客户端的JSON请求,并使用API发送/接收SOAP。但是,对客户端的所有响应都是空的
我收到非空响应的唯一情况是,我的JSON请求存在验证错误
下面是一些可能相关的代码
app = Application([MyServer],
MY_NAMESPACE,
in_protocol=JsonDocument(validator='soft'),
out_protocol=JsonDocument())
application_server = csrf_exempt(MyDjangoApplication(app))
Mydjango应用程序的定义
class MyDjangoApplication(DjangoApplication):
def __call__(self, request, *args, **kwargs):
retval = self.HttpResponseObject()
def start_response(status, headers):
# Status is one of spyne.const.http
status, reason = status.split(' ', 1)
retval.status_code = int(status)
for header, value in headers:
retval[header] = value
environ = request.META.copy()
if request.method == 'POST':
response = self.handle_rpc(environ, start_response)
else:
home_path = reverse('proxy:list_method')
uri = MY_ENDPOINT_URL or request.build_absolute_uri(home_path)
# to generate wsdl content
response = self._WsgiApplication__handle_wsdl_request(environ, start_response, uri)
if request.path == home_path and _is_wsdl_request(environ):
fn = None
elif 'method_name' in kwargs:
fn = view_method
else:
fn = list_method
if fn:
return fn(request, app=self, *args, **kwargs)
self.set_response(retval, response)
return retval
MyServer的定义
class MyServer(ServiceBase):
@rpc(MyTestMethodRequest, Sign, **method(_returns=MyTestMethodResponse))
@check_method()
def TestMethod(ctx, request, signature):
response = {
'Data': "test"
}
return response
MyTestMethodRequest、MyTestMethodResponse的定义:
class MyTestMethodRequest(ComplexModel):
__namespace__ = MY_NAMESPACE
MyString = String(encoding=STR_ENCODING)
class MyTestMethodResponse(ComplexModel):
__namespace__ = MY_NAMESPACE
Data = String(encoding=STR_ENCODING)
检查方法的定义:
def check_method(error_handler=None):
def _check_method(func):
method_name = func.__name__
def __check_method(ctx, request, signature, *args, **kwargs):
if hasattr(request, '__dict__'):
request = request.__dict__
if hasattr(signature, '__dict__'):
signature = signature.__dict__
response = func(ctx, request or {}, signature or {}, *args, **kwargs)
# setting output protocol
output_message = generate_out_string(ctx, [response])
return response
__check_method.__name__ = method_name
__check_method.__doc__ = func.__doc__
return __check_method
return _check_method
生成输出字符串的定义:
def generate_out_string(ctx, objects):
ctx.out_protocol = ctx.in_protocol
return _generate_out_string(ctx, objects)
def _generate_out_string(ctx, objects):
protocol = ctx.out_protocol
ctx.out_object = objects
protocol.serialize(ctx, protocol.RESPONSE)
protocol.create_out_string(ctx)
out_string = list(ctx.out_string)
return out_string[0] if out_string else ''
注意:这些定义中的大多数都被简化了(我删除了一些我认为不相关的行)看看你发布的代码,我不能说我理解所有这些围绕参数的附加修饰符和修饰符有什么用 删除它们可以解决所有问题 因此,让我们:
class MyTestMethodRequest(ComplexModel):
__namespace__ = MY_NAMESPACE
MyString = Unicode
class MyTestMethodResponse(ComplexModel):
__namespace__ = MY_NAMESPACE
Data = Unicode
假设您有以下服务:
class MyService(ServiceBase):
@rpc(MyTestMethodRequest, Sign, _returns=MyTestMethodResponse)
def TestMethod(ctx, request, signature):
return MyTestMethodResponse(data="test")
你可以有:
app_json = Application([MyService],
MY_NAMESPACE,
in_protocol=JsonDocument(validator='soft'),
out_protocol=JsonDocument())
及
您可以像往常一样将其传递给DjangoApplication
:
app_json_dja = csrf_exempt(DjangoApplication(app_json))
app_soap_dja = csrf_exempt(DjangoApplication(app_soap))
然后,您可以将其装载到Django的url路由器中
我希望这有帮助 当您使用普通应用程序时,它工作吗?到底怎么了?谢谢你的回复。使用默认的django应用程序会产生相同的结果。我不知道为什么使用自定义Django应用程序的全部细节,但我想主要原因是,它只会处理“POST”请求,否则会显示自定义页面。好的,请发布服务代码,因为您的DjangoApplication子类与此无关,并且应用程序实例化看起来很好。如果是bug,我将修复它,如果不是,我将告诉您如何修复它,但首先我必须复制它。已更新。不确定这些是否是您需要的代码。