Use FormData and multipart/form-data instead of application/urlencoded for requests

This commit is contained in:
Mahdi Dibaiee 2015-07-02 05:10:13 +04:30
parent 5f07eaa456
commit 4591ced347
4 changed files with 122 additions and 52 deletions

View File

@ -16,35 +16,95 @@ var _qs = require('qs');
var _qs2 = _interopRequireDefault(_qs);
function fetch(path, data) {
var post = _qs2['default'].stringify(data);
var _formData = require('form-data');
var _formData2 = _interopRequireDefault(_formData);
function fetch(path) {
var data = arguments[1] === undefined ? { test: 1 } : arguments[1];
var form = new _formData2['default']();
var keys = Object.keys(data);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
console.log(key, data[key]);
form.append(key, data[key]);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return new Promise(function (resolve, reject) {
var req = _https2['default'].request({
hostname: 'api.telegram.org',
method: data ? 'POST' : 'GET',
form.getLength(function (err, length) {
if (err) {
return reject(err);
}
form.submit({
protocol: 'https:',
host: 'api.telegram.org',
path: '/bot' + path,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Length': length
}
}, function (response) {
return getBody(response).then(function (res) {
}, function (error, response) {
if (error) {
return reject(error);
}
return getBody(response).then(function (body) {
try {
var json = JSON.parse(res);
var json = JSON.parse(body);
resolve(json);
} catch (e) {
reject(e);
}
})['catch'](reject);
});
if (post) {
req.write(post);
}
req.end();
})['catch'](function (err) {
console.error('Error sending request', err);
});
});
// console.log(form.getHeaders());
// return new Promise((resolve, reject) => {
// const req = https.request({
// hostname: 'api.telegram.org',
// method: keys.length ? 'POST' : 'GET',
// path: '/bot' + path,
// headers: form.getHeaders()
// }, response => {
// return getBody(response).then(res => {
// console.log(res);
// try {
// let json = JSON.parse(res);
// resolve(json);
// } catch(e) {
// reject(e);
// }
// }).catch(reject);
// });
//
// form.pipe(req);
// }).catch(err => {
// console.error('Error sending request', err);
// });
}
function getBody(stream) {

View File

@ -5,7 +5,7 @@ var Message = require('telegram-api/types/Message');
var Question = require('telegram-api/types/Question');
var bot = new Bot({
token: 'YOUR_KEY'
token: '121143906:AAE6pcpBoARNZZjr3fUpvKuLInJ5Eee5Ajk'
});
bot.start().catch(err => {

View File

@ -1,34 +1,43 @@
import https from 'https';
import qs from 'qs';
import FormData from 'form-data';
export default function fetch(path, data = {}) {
const form = new FormData();
const keys = Object.keys(data);
for (let key of keys) {
console.log(key, data[key]);
form.append(key, data[key]);
}
export default function fetch(path, data) {
const post = qs.stringify(data);
return new Promise((resolve, reject) => {
const req = https.request({
hostname: 'api.telegram.org',
method: data ? 'POST' : 'GET',
form.getLength((err, length) => {
if (err) {
return reject(err);
}
form.submit({
protocol: 'https:',
host: 'api.telegram.org',
path: '/bot' + path,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Length': length
}
}, response => {
return getBody(response).then(res => {
}, (error, response) => {
if (error) {
return reject(error);
}
return getBody(response).then(body => {
try {
let json = JSON.parse(res);
let json = JSON.parse(body);
resolve(json);
} catch(e) {
reject(e);
}
}).catch(reject);
});
if (post) {
req.write(post);
}
req.end();
}).catch(err => {
console.error('Error sending request', err);
});
});
}

View File

@ -1,6 +1,6 @@
{
"name": "telegram-api",
"version": "0.3.3",
"version": "0.3.4",
"description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js",
"scripts": {
@ -31,6 +31,7 @@
"homepage": "https://github.com/mdibaiee/node-telegram-api",
"dependencies": {
"babel": "^5.6.14",
"form-data": "^1.0.0-rc1",
"grunt": "^0.4.5",
"grunt-babel": "^5.0.1",
"grunt-contrib-symlink": "^0.3.0",