node.js - Retrieve both string and json of body using koa js -
i use koa js bodyparser, suppose client send body this:
{ "first": "1" , "second": "2"}
what want original body string no changes (json.stringify changes order of fields , remove spaces can't use it). try use raw-body gives me string of body, have parse json.
is there middleware give me body both json , original string?
if want both raw string , json, string, keep copy, parse json.
var getrawbody = require('raw-body') app.use(function* (next) { var string = yield getrawbody(this.req, { length: this.length, limit: '1mb', encoding: this.charset }) var json = json.parse(string) // "string" // "json" })
note: have run getrawbody()
against this.req
, since that's node's raw http request
object. this.request
koa-specific , won't work.
Comments
Post a Comment