json - How to produce/consume XML using swagger-node -


i'm new @ using swagger-node (swagger-spec 2.0) , have need api consume , produce both xml , json (because that's customer wants). have focused on "produce" part.

when producing response, know can turn js object xml using tools such jstoxml or easyxml. question is: necessary when using swagger-node or tools suppose handle this? guess need in controller code should return.

for example, create new project using swagger swagger project create myproject (choose express framework)

change yaml file /hello api get: returns both json or xml

paths:   /hello:     # binds a127 app logic route     x-swagger-router-controller: hello_world     get:       description: returns 'hello' caller       # used method name of controller       operationid: hello       produces:         - application/json         - application/xml 

then change hello_world.js controller return json object instead of string

  // variables defined in swagger document can referenced using req.swagger.params.{parameter_name}   var name = req.swagger.params.name.value || 'stranger';   var hello = util.format('hello, %s!', name);    // sends json response single string   res.json({message:hello}); } 

when start project , use postman header accept = application/json response:

{     "message": "hello, stranger!" } 

if change header accept application/xml, still json response, not xml. hoping see is:

<object> <message>hello, stranger!</message> </object> 

i know code wrong use res.json() because believe sets content-type application/json.

i don't know else use produce xml response. when change out res.json() use easyxml

var xml = easyxml.render({message:hello}); res.type('xml').end(xml); 

i validation error swagger:

[   {     "status": 500,     "message": "response validation failed: value expected array/object not"   } ] 

so how should controller formatting response return either xml or json?

let's dust 1 , answer shall we! i'm not sure easyxml doing or why it's not working, jstoxml works great:

var jstoxml = require('jstoxml'); var express = require('express'); var util = require('util'); var app = express();  app.get('/', function(req, res) {     var name = 'stranger';     var hello = {         object: {             message: util.format('hello, %s!', name)         }     };     if (req.headers.accept === 'application/xml') {         res.type('xml')         res.end(jstoxml.toxml(hello));     } else {         res.json(hello);     } });  app.listen(process.env.port || 8100); 

accept: application/json

{     "object":      {         "message": "hello, stranger!"     } } 

accept: application/xml

<object>     <message>hello, stranger!</message> </object> 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -