You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
@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.
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.
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
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.
Activity
MBrouns commentedon Mar 2, 2015
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.
randallmeeker commentedon Mar 2, 2015
http://stackoverflow.com/questions/28464786/custom-xml-response-in-sails-js-blueprint-for-a-model
cage1618 commentedon Mar 2, 2015
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 commentedon Mar 3, 2015
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 commentedon Mar 6, 2015
@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:then install Skipper for everything that's not XML:
then in your
config/http.js
add:You can add this as a key directly under
module.exports.http
. That should get you parsing both XML and other requests.leoliew commentedon Mar 10, 2015
thanks!
simbacai commentedon Jul 20, 2015
@sgress454 awesome, i met this issue also, thx for your sharing
moisesfr commentedon Mar 30, 2016
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 commentedon Apr 5, 2016
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 commentedon May 31, 2016
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 commentedon May 31, 2016
OK, i did overlook this part commented in config/http.js
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.