node.js - Returning XML in response from Loopback Remote Method -
i using loopback connector rest (1.9.0) , have remote method 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 returns escaped json string:
"<foo xmlns=\"bar\"/>"
instead of
<foo xmlns="bar"/>
note response have content-type set text/xml.
if set accept: header "text/xml", "not acceptable" response.
if set
"rest": { "normalizehttppath": false, "xml": true }
in config.json, 500 error:
syntaxerror: unexpected token <
i think "xml: true" property causing response parser try convert json xml.
how can loopback return xml in response without parsing it? problem setting return type "string"? if so, type loopback recognize xml?
you need set toxml in response object (more on in bit). first, set return type 'object' 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, need return json object toxml property. toxml should function returns string representation of xml. if set accept header "text/xml" response should xml. see below:
foo.getxml = function(cb) { cb(null, { toxml: function() { return '<something></something>'; } }); };
you still need enable xml in config.json because it's disabled default:
"remoting": { "rest": { "normalizehttppath": false, "xml": true } }
see https://github.com/strongloop/strong-remoting/blob/4b74a612d3c969d15e3dcc4a6d978aa0514cd9e6/lib/http-context.js#l393 more info on how works under hood.
Comments
Post a Comment