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); var _qs2 = _interopRequireDefault(_qs);
function fetch(path, data) { var _formData = require('form-data');
var post = _qs2['default'].stringify(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) { return new Promise(function (resolve, reject) {
var req = _https2['default'].request({ form.getLength(function (err, length) {
hostname: 'api.telegram.org', if (err) {
method: data ? 'POST' : 'GET', return reject(err);
}
form.submit({
protocol: 'https:',
host: 'api.telegram.org',
path: '/bot' + path, path: '/bot' + path,
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Length': length
} }
}, function (response) { }, function (error, response) {
return getBody(response).then(function (res) { if (error) {
return reject(error);
}
return getBody(response).then(function (body) {
try { try {
var json = JSON.parse(res); var json = JSON.parse(body);
resolve(json); resolve(json);
} catch (e) { } catch (e) {
reject(e); reject(e);
} }
})['catch'](reject); })['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) { function getBody(stream) {

View File

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

View File

@ -1,34 +1,43 @@
import https from 'https'; import FormData from 'form-data';
import qs from 'qs';
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) => { return new Promise((resolve, reject) => {
const req = https.request({ form.getLength((err, length) => {
hostname: 'api.telegram.org', if (err) {
method: data ? 'POST' : 'GET', return reject(err);
}
form.submit({
protocol: 'https:',
host: 'api.telegram.org',
path: '/bot' + path, path: '/bot' + path,
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded' 'Content-Length': length
} }
}, response => { }, (error, response) => {
return getBody(response).then(res => { if (error) {
return reject(error);
}
return getBody(response).then(body => {
try { try {
let json = JSON.parse(res); let json = JSON.parse(body);
resolve(json); resolve(json);
} catch(e) { } catch(e) {
reject(e); reject(e);
} }
}).catch(reject); }).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", "name": "telegram-api",
"version": "0.3.3", "version": "0.3.4",
"description": "Control Telegram bots easily using the new Telegram API", "description": "Control Telegram bots easily using the new Telegram API",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -31,6 +31,7 @@
"homepage": "https://github.com/mdibaiee/node-telegram-api", "homepage": "https://github.com/mdibaiee/node-telegram-api",
"dependencies": { "dependencies": {
"babel": "^5.6.14", "babel": "^5.6.14",
"form-data": "^1.0.0-rc1",
"grunt": "^0.4.5", "grunt": "^0.4.5",
"grunt-babel": "^5.0.1", "grunt-babel": "^5.0.1",
"grunt-contrib-symlink": "^0.3.0", "grunt-contrib-symlink": "^0.3.0",