Returning XML in response from Loopback Remote Method

后端 未结 2 1350
遥遥无期
遥遥无期 2021-01-23 08:24

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

   Foo.remoteMethod
   (  
      \"getXml\",
      {
         accepts:          


        
2条回答
  •  灰色年华
    2021-01-23 08:30

    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 '';
        }
      });
    };
    

    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.

提交回复
热议问题