Skip to content

how can I get xml data from request in sails.js? #2714

Closed
@cage1618

Description

@cage1618

how can I get xml data from request in sails.js?

thanks!

Activity

MBrouns

MBrouns commented on Mar 2, 2015

@MBrouns

I dont think sails supports this out of the box but I guess you could override the default toJSON method of your model to return xml instead of json.

cage1618

cage1618 commented on Mar 2, 2015

@cage1618
Author

oh, I think we have some differences understanding about this question, I does't want to do a xml response.

I use sails create my server-side, some out request will post some data to my api with xml format, so I want to get this xml data in my api controller.

thanks!

cage1618

cage1618 commented on Mar 3, 2015

@cage1618
Author

Don't hass anyone answer this question?

I got an error in the req object: _multipartyError: [Error: unrecognized content-type: text/xml]

_fileparser:
{ req: [Circular],
next: [Function],
options: { maxTimeToWaitForFirstFile: 10000, maxTimeToBuffer: 4500 },
upstreams: [],
textParams: [],
form:
{ _writableState: [Object],
writable: true,
domain: null,
_events: [Object],
_maxListeners: undefined,
error: [Error: unrecognized content-type: text/xml],
finished: false,
autoFields: false,
autoFiles: false,
maxFields: 1000,
maxFieldsSize: 2097152,
maxFilesSize: Infinity,
uploadDir: '/tmp',
encoding: 'utf8',
hash: false,
bytesReceived: 0,
bytesExpected: 52,
openedFiles: [],
totalFieldSize: 0,
totalFieldCount: 0,
totalFileSize: 0,
flushing: 0,
backpressure: false,
writeCbs: [],
handleError: [Function: handleError] },
_events: { firstFile: [Object], warning: [Function] },
_multipartyError: [Error: unrecognized content-type: text/xml],
_hasPassedControlToApp: true },

sgress454

sgress454 commented on Mar 6, 2015

@sgress454
Member

@cage1618 Thanks for using Sails! FYI this forum is for posting bugs in the Sails core. In the future please post support queries to StackOverflow, our Google Group or our Gitter Chat.


Sails doesn't support parsing XML bodies natively (nor does Express). However, you can do it fairly easily by configuring some things in config/http.js. First install an XML body parser:

npm install express-xml-bodyparser --save

then install Skipper for everything that's not XML:

npm install skipper --save

then in your config/http.js add:

  bodyParser: function(opts) {
    // Get an XML parser instance
    var xmlParser = require('express-xml-bodyparser')(opts);
    // Get a Skipper instance (handles URLencoded, JSON-encoded and multipart)
    var skipper = require('skipper')(opts);
    // Return a custom middleware function
    return function(req, res, next) {
      // If it looks like XML, parse it as XML
      if (req.headers && (req.headers['content-type'] == 'text/xml' || req.headers['content-type'] == 'application/xml')) {
        return xmlParser(req, res, next);
      }
      // Otherwise let Skipper handle it
      return skipper(req, res, next);
    };

  }

You can add this as a key directly under module.exports.http. That should get you parsing both XML and other requests.

leoliew

leoliew commented on Mar 10, 2015

@leoliew

thanks!

simbacai

simbacai commented on Jul 20, 2015

@simbacai

@sgress454 awesome, i met this issue also, thx for your sharing

moisesfr

moisesfr commented on Mar 30, 2016

@moisesfr

Hello,

It seems this error still persists (Node.js v4.2.3, Sails.js v0.11.3).

We've tried the proposed workaround (and a bunch more) but they didn't solve it.
Any more ideas or this is just with us?

sgress454

sgress454 commented on Apr 5, 2016

@sgress454
Member

Hi @moisesfr, the underlying issues with Sails config that required different bodyparser workarounds have been fixed as of version 0.12.2. You can check out an example of doing XML parsing at https://github.com/sails101/use-xml-body-parser.

cybercow

cybercow commented on May 31, 2016

@cybercow

Hi all, im trying to do as suggested and used the: https://github.com/sails101/use-xml-body-parser repository as example but when i lift sails i get the error that the skipper module is not present:

info: Starting app... module.js:340 throw err; ^ Error: Cannot find module 'skipper' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at module.exports.http.middleware.bodyParser (/var/www/current/use-xml-body-parser/config/http.js:79:21) ....

I'm on Debian 8, and using sails 0.12.3. Any help would be appreciated. Thank you, and congratulations for making such a great fw as Sails.js

cybercow

cybercow commented on May 31, 2016

@cybercow

OK, i did overlook this part commented in config/http.js

Note that Sails uses an internal instance of Skipper by default; to override it and specify more options, make sure to "npm install skipper" in your project first. You can also specify a different body parser or a custom function with req, res and next parameters (just like any other middleware function).

My first though was that as skipper is built-in by default, no extra install would be needed ... so i was confused should i install it manually or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sgress454@cybercow@randallmeeker@cage1618@leoliew

        Issue actions

          how can I get xml data from request in sails.js? · Issue #2714 · balderdashy/sails