Skip to content
Snippets Groups Projects
Commit 2c47d357 authored by aurieh's avatar aurieh
Browse files

Fix body parser

parent 5eac844a
No related branches found
No related tags found
No related merge requests found
...@@ -46,34 +46,37 @@ app.use(function* (next) { ...@@ -46,34 +46,37 @@ app.use(function* (next) {
yield next; yield next;
return; return;
} }
if (!this.headers['application/json']) { if (this.headers['content-type'] || !~this.headers['content-type'].indexOf('application/json')) {
this.status = 400; this.status = 400;
this.body = { this.body = {
code: 400, code: 400,
message: 'invalid content type, should be application/json' message: 'invalid content type, should be application/json'
}; };
yield next; return yield next;
return;
} }
let rawData = ''; try {
const body = yield new Promise((resolve, reject) => {
this.req.on('data', chunk => { let rawData = '';
rawData += chunk.toString(); this.req.on('data', chunk => {
}); rawData += chunk.toString();
this.req.on('end', () => { });
try { this.req.on('end', () => {
this.req.body = JSON.parse(rawData); try {
next(); resolve(JSON.parse(rawData));
return; } catch (err) {
} catch (err) { reject({
this.body = { code: 400,
code: 400, message: 'invalid body data, should be a valid JSON string'
message: 'invalid body data, should be a valid JSON string' });
}; }
this.status = 400; });
} });
}); this.req.body = body;
} catch (e) {
this.status = 400;
this.body = e;
}
}); });
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment