bodyparser.js 661 B

1234567891011121314151617181920212223
  1. const bodyParser = require('koa-bodyparser')
  2. /**
  3. * 这里做一个兼容
  4. * 由于微信方面客服接口 post 过来的数据是 JSON
  5. * 但是 header 里的 Content-Type 却错误标记为 text/xml
  6. * 这里对所有 Content-Type 为 text/xml 的用 JSON 解析
  7. * 如果你在消息推送后台配置的是 xml,请直接将 detectJSON 设置为空
  8. */
  9. module.exports = (opts = {}) => {
  10. const options = Object.assign({}, {
  11. detectJSON (ctx) {
  12. if (ctx.request.type === 'text/xml') {
  13. return true
  14. } else {
  15. return false
  16. }
  17. }
  18. }, opts)
  19. return bodyParser(options)
  20. }