How to access the raw body of the request before bodyparser?

后端 未结 1 623
长情又很酷
长情又很酷 2021-01-12 11:33

I am writing a custom middleware that generates a cryptographic signature of every request (it is very similiar to the authentication mechanism used by AWS API v4). In order

相关标签:
1条回答
  • 2021-01-12 12:10

    Ok, since the only feasible way to solve this problem seems to be by modifying the original source code of bodyParser, I have forked it.

    https://github.com/emanuelecasadio/body-parser-rawbody

    This fork exposes the raw body of the request as a field named rawBody. As you can see, there is only ONE extra line of code.

    You can install it by using npm install body-parser-rawbody.

    EDIT

    Another option is to use the bodyParser like this, as noted by dougwilson here: https://github.com/expressjs/body-parser/issues/83#issuecomment-80784100

    app.use(bodyParser.json({verify:function(req,res,buf){req.rawBody=buf}}))
    

    I haven't personally tried this option and I do not know if it works.

    0 讨论(0)
提交回复
热议问题