mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-22 14:03:28 -07:00
Add support for JSON POST
Add support for JSON POST
This commit is contained in:
parent
ad7702aaf4
commit
1aff82242a
1 changed files with 14 additions and 1 deletions
|
@ -52,6 +52,7 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
var _this = this;
|
||||
var buffer = '';
|
||||
var cancelled = false;
|
||||
var ct = request.headers['content-type'];
|
||||
|
||||
// What to do when done
|
||||
var onSuccess = function () {
|
||||
|
@ -65,6 +66,19 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
//added support for json data
|
||||
if (ct && ct.split(';')[0] === 'application/json') {
|
||||
try {
|
||||
let json = JSON.parse(buffer);
|
||||
buffer = json.data;
|
||||
} catch(e) {
|
||||
winston.verbose('error adding document');
|
||||
response.writeHead(500, { 'content-type': 'application/json' });
|
||||
response.end(JSON.stringify({ message: 'invalid JSON received.' }));
|
||||
}
|
||||
}
|
||||
|
||||
// And then save if we should
|
||||
_this.chooseKey(function (key) {
|
||||
_this.store.set(key, buffer, function (res) {
|
||||
|
@ -83,7 +97,6 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
|||
};
|
||||
|
||||
// If we should, parse a form to grab the data
|
||||
var ct = request.headers['content-type'];
|
||||
if (ct && ct.split(';')[0] === 'multipart/form-data') {
|
||||
var busboy = new Busboy({ headers: request.headers });
|
||||
busboy.on('field', function (fieldname, val) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue