Co-body is a module written by TJ Daniel that uses Generator
parsing forms.
Currently co-body
only supported: application/json
, application/x-www-form-urlencoded
and, and text/plain
sometimes we need to receive XML format data (its type is text/xml
), you can increase its support for XML in the following ways:
- Copy the file in the Lib directory
json.js
, renamed textxml.js
, changed to, that is, do try{ done(null, JSON.parse(str); }
try{ done(null, str); }
not do any parsing, directly save.
- After modifying the root directory
any.js
, add the var text = require(‘./textxml‘);
JSON parsing logic in the exports function and add such a sentence if (‘text/xml‘ == type) return text(req, opts);
to support other text types and modify it slightly.
- Modify the root directory to
index.js
add exports.text = require(‘./lib/textxml‘);
, for other programs to call.
In the use koa-body
of such included co-body
tools, to support XML format, you may need further modifications.
koa-body
For example, to modify the file under its root directory index.js
function requestbody
, add the following in the appropriate location:
<!-- lang: js -->else if (this.is(‘text/xml‘)) { // 返回对应的Content-Type,若无效,为false body = yield buddy.text(this, {encoding: opts.encoding, limit: opts.jsonLimit});}
In this way, the XML string is fully received in plain text for further processing.
This scenario applies to push messages for the server.
Add support for text types such as XML for Co-body