从Loopback Remote Method返回XML [英] Returning XML in response from Loopback Remote Method

查看:284
本文介绍了从Loopback Remote Method返回XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Loopback Connector REST(1.9.0),并有一个返回XML的远程方法:

  Foo。 remoteMethod 

getXml,
{
接受:[
{arg:'id',type:'string',required:true}
],
http:{path:'/:id / content',动词:'get'},
返回:{type:string,root:true},
rest:{after:setContentType(text / xml)}
}

响应始终返回转义的JSON字符串:

 < foo xmlns = \ bar\ /> 中

而不是

 code>< foo xmlns =bar/> 

请注意,响应的内容类型设置为text / xml。



如果我将Accept:header设置为text / xml,我总是会收到Not Acceptable作为响应。



如果我设置

 rest:{
normalizeHttpPath:false,
xml true
}

在config.json中,我收到一个500错误:

  SyntaxError:Unexpected token< 

我认为xml:true属性只是引起响应解析器来尝试转换JSON转换成XML。



如何让Loopback在响应中返回XML而不解析?问题是我将返回类型设置为string?如果是这样,Loopback会将其识别为XML?

解决方案

您需要在响应对象中设置为XML(更多在那一点)。首先,将返回类型设置为'object',如下所示:

  Foo.remoteMethod 

getXml,
{
接受:[
{arg:'id',type:'string',required:true}
],
http:路径:'/:id / content',动词:'get'},
返回:{type:object,root:true},
rest:{after setContentType(text / xml)}
}

您将需要使用toXML作为唯一属性返回一个JSON对象。 toXML应该是返回XML的字符串表示形式的函数。如果将Accept标题设置为text / xml,则响应应为XML。见下文:

  Foo.getXml = function(cb){
cb(null,{
toXML :function(){
return'< something>< / something>';
}
});
};

您仍然需要在config.json中启用XML,因为默认情况下禁用:

$
$$$$$$$ :true
}
}

请参阅 https://github.com/strongloop/strong-remoting/blob/4b74a612d3c969d15e3dcc4a6d978aa0514cd9e6/lib /http-context.js#L393 ,了解更多关于这个工作原理的信息。


I am using the Loopback Connector REST (1.9.0) and have a remote method that returns XML:

   Foo.remoteMethod
   (  
      "getXml",
      {
         accepts: [            
            {arg: 'id', type: 'string', required: true }
         ],
         http: {path: '/:id/content', "verb": 'get'},
         returns: {"type": "string", root:true},
         rest: {"after": setContentType("text/xml") }         
      }
   )

The response always returns an escaped JSON string:

"<foo xmlns=\"bar\"/>" 

instead of

<foo xmlns="bar"/>

Note that the response does have the content-type set to text/xml.

If I set my Accept: header to "text/xml", I always get "Not Acceptable" as a response.

If I set

"rest": {
  "normalizeHttpPath": false,
  "xml": true
}

In config.json, then I get a 500 error:

SyntaxError: Unexpected token <

I think that the "xml: true" property is simply causing a response parser to try to convert JSON into XML.

How can I get Loopback to return the XML in the response without parsing it? Is the problem that I am setting the return type to "string"? If so, what it the type that Loopback would recognize as XML?

解决方案

You need to set toXML in your response object (more on that in a bit). First, set the return type to 'object' as shown below:

Foo.remoteMethod
(  
  "getXml",
  {
     accepts: [            
        {arg: 'id', type: 'string', required: true }
     ],
     http: {path: '/:id/content', "verb": 'get'},
     returns: {"type": "object", root:true},
     rest: {"after": setContentType("text/xml") }         
  }
)

Next, you will need to return a JSON object with toXML as the only property. toXML should be a function that returns a string representation of your XML. If you set the Accept header to "text/xml" then the response should be XML. See below:

Foo.getXml = function(cb) {
  cb(null, {
    toXML: function() {
      return '<something></something>';
    }
  });
};

You still need to enable XML in config.json because it's disabled by default:

"remoting": {
  "rest": {
    "normalizeHttpPath": false,
    "xml": true
  }
}

See https://github.com/strongloop/strong-remoting/blob/4b74a612d3c969d15e3dcc4a6d978aa0514cd9e6/lib/http-context.js#L393 for more info on how this works under the hood.

这篇关于从Loopback Remote Method返回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆