Add support for text types such as XML for Co-body

Source: Internet
Author: User

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-bodyFor 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.