-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raw data? #43
Comments
Wow, you guys work fast. Unfortunately I wasn't clear enough.... I'm integrating with Shopify webhooks specifically. They send a POST request with a JSON body. bodyparser works great for this. The problem is that I have to validate the HMAC, and to do so I have to get the raw_body of the response as well. So, I need the parsed body and the raw body. One for verification and the other for data. |
Get the @internalfx Please give us a full example codes? |
My process is further down the middleware pipeline. Be patient with me, my understanding of the internals of Here was my cheap hack on line 38 of
I also modified
Now in my route policy, something very similar to a
|
@internalfx this module is made for generic usage. you can try https://github.com/stream-utils/raw-body to get the body content and parse by yourself. :) |
I understand if this is not where you want to take the library. The modifications I've made have solved the problem. I didn't want to give up the automatic JSON detection and parsing. I also wanted the form parsing that is built in. I may just release a fork and track changes to bodyparser, while providing an option for raw data to myself and others. Thanks guys. |
I need to keep raw data too. |
the code in body parser should be changed to function* parseBody(ctx) {
if (enableJson && ((detectJSON && detectJSON(ctx)) || ctx.request.is(jsonTypes))) {
let result = yield parse.json(ctx, jsonOpts);
if (result.body) {
ctx.request.raw_body = result.raw
return result.body
} else {
return result
}
}
if (enableForm && ctx.request.is(formTypes)) {
return yield parse.form(ctx, formOpts);
}
if (enableText && ctx.request.is(textTypes)) {
return yield parse.text(ctx, textOpts) || '';
}
return {};
}
}; otherwise you will lose the ctx.request.body information |
I have seen there is a verify option in app.use(bodyparser.json({
verify: function(req, res, buf) {
req.rawBody = buf;
},
})); @fengmk2 @dead-horse Should we support something like that? Or just avoid this middleware and rewrite the body parsing logic based on raw-body by myself? |
I'd like to add a options to let co-body return the raw body. |
then we can access the request's raw body with |
Sounds great. How could we name this option? rawBody: true? It will be cool if we can access raw body (ctx.request.rawBody) and parsed body (ctx.request.body) at the same time.
|
the |
请问 --data '{"ipv4":"192.168.1.1","note":"开发","ftp_user":"d1y","ftp_pwd":"middle","ftp_path":""}' 理论上, 我应该是要得到一个对象的 但实际上我在 body: { '{"ipv4":"192': { '168': [ [Object] ] } }
|
@d1y Please use English that your message can be valuable to other people all over the world. https://github.com/koajs/bodyparser/blob/master/index.js#L78 https://github.com/koajs/bodyparser/blob/master/index.js#L91 Welcome to Node.js v13.2.0.
Type ".help" for more information.
> JSON.parse('{"ipv4":"192.168.1.1","note":"开发","ftp_user":"d1y","ftp_pwd":"middle","ftp_path":""}')
{
ipv4: '192.168.1.1',
note: '开发',
ftp_user: 'd1y',
ftp_pwd: 'middle',
ftp_path: ''
} So the problem is not about the bodyparser. Seems like a kind of serialization problem on your client ,or your upstream middleware modified your ctx.request.body . Please check your code again, or provide a runnable demo for reproducing and debugging the problem. |
@d1y I have got the key. |
Is there any easy way to get to the raw request body. Or is adding such a feature a possibility?
The text was updated successfully, but these errors were encountered: