本文整理汇总了TypeScript中needle.get函数的典型用法代码示例。如果您正苦于以下问题:TypeScript get函数的具体用法?TypeScript get怎么用?TypeScript get使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: ResponsePipeline
function ResponsePipeline() {
needle.get('http://stackoverflow.com/feeds', { compressed: true, headers: {
Authorization: 'bearer 12dsfgsdgsgq'
} }, function (err, resp) {
console.log(resp.body); // this little guy won't be a Gzipped binary blob
// but a nice object containing all the latest entries
});
var options = {
compressed: true,
follow: 5,
rejectUnauthorized: true
};
// in this case, we'll ask Needle to follow redirects (disabled by default),
// but also to verify their SSL certificates when connecting.
var stream = needle.get('https://backend.server.com/everything.html', options);
stream.on('readable', function () {
var data: any;
while (data = this.read()) {
console.log(data.toString());
}
});
}
示例2: ResponsePipeline
function ResponsePipeline() {
needle.get('http://stackoverflow.com/feeds', { compressed: true }, function (err, resp) {
console.log(resp.body); // this little guy won't be a Gzipped binary blob
// but a nice object containing all the latest entries
});
var options = {
compressed: true,
follow: 5,
rejectUnauthorized: true
};
// in this case, we'll ask Needle to follow redirects (disabled by default),
// but also to verify their SSL certificates when connecting.
var stream = needle.get('https://backend.server.com/everything.html', options);
stream.on('readable', function () {
var data: any;
while (data = stream.read()) {
console.log(data.toString());
}
});
stream.on('end', function(err: any) {
// if our request had an error, our 'end' event will tell us.
if (!err) console.log('Great success!');
})
}
示例3: HttpGetWithBasicAuth
function HttpGetWithBasicAuth() {
needle.get('https://api.server.com', { username: 'you', password: 'secret' }, function(err, resp) {
// used HTTP auth
});
needle.get('https://username:password@api.server.com', function(err, resp) {
// used HTTP auth from URL
});
}
示例4: Usage
function Usage() {
// using callback
needle.get('http://ifconfig.me/all.json', function (error, response) {
if (!error)
console.log(response.body.ip_addr); // JSON decoding magic. :)
});
// using streams
var out: any; // = fs.createWriteStream('logo.png');
needle.get('https://google.com/images/logo.png').pipe(out);
}
示例5: Usage
function Usage() {
// using promises
needle('get', 'http://ifconfig.me/all.json')
.then((resp) => console.log(resp.body.ip_addr));
// using callback
needle.get('http://ifconfig.me/all.json', (error, response) => {
if (!error)
console.log(response.body.ip_addr); // JSON decoding magic. :)
});
// using streams
const out = fs.createWriteStream('file.txt');
needle.get('https://google.com/images/logo.png').pipe(out);
}
示例6: reject
let promise = new Promise<SubscriptionAccount>((resolve, reject) => {
needle.get(sogouSearchUrl, options, (error: any, httpResponse: any) => {
if (error) {
reject('Failed to retrieve content from the given url.');
return;
}
if (httpResponse.statusCode === 200) {
// Successfully retrieve the result.
let responseHTML = cheerio.load(httpResponse.body);
let subscriptionItem = responseHTML(AccountSelectors.BASE);
while (subscriptionItem.length > 0) {
if (subscriptionItem.find(AccountSelectors.WECHAT_ID).length > 0) {
let resultWechatId = subscriptionItem.find(AccountSelectors.WECHAT_ID).text();
if (resultWechatId !== this.wechatId) {
subscriptionItem = subscriptionItem.next();
continue;
}
let name = subscriptionItem.find(AccountSelectors.NAME).text();
let resultUrl = subscriptionItem.attr('href');
let accountImage = subscriptionItem.find(AccountSelectors.IMG).attr('src');
let account = new SubscriptionAccount(name, resultUrl, accountImage);
resolve(account);
return;
}
subscriptionItem = subscriptionItem.next();
}
}
reject(httpResponse.body);
});
});
示例7: Various
function Various() {
// using promises
needle('get', 'https://news.ycombinator.com/rss')
.then((resp) => {
// if xml2js is installed, you'll get a nice object containing the nodes in the RSS
});
needle('get', 'http://upload.server.com/tux.png', { output: '/tmp/tux.png' })
.then((resp) => {
// you can dump any response to a file, not only binaries.
});
needle('get', 'http://search.npmjs.org', { proxy: 'http://localhost:1234' })
.then((resp) => {
// request passed through proxy
});
// using callback
needle.get('https://news.ycombinator.com/rss', (err, resp, body) => {
// if xml2js is installed, you'll get a nice object containing the nodes in the RSS
});
needle.get('http://upload.server.com/tux.png', { output: '/tmp/tux.png' }, (err, resp, body) => {
// you can dump any response to a file, not only binaries.
});
needle.get('http://search.npmjs.org', { proxy: 'http://localhost:1234' }, (err, resp, body) => {
// request passed through proxy
});
// using streams
const stream1 = needle.get('http://www.as35662.net/100.log');
stream1.on('readable', () => {
let chunk: any;
while (chunk = stream1.read()) {
console.log('got data: ', chunk);
}
});
const stream2 = needle.get('http://jsonplaceholder.typicode.com/db', { parse: true });
stream2.on('readable', () => {
let node: any;
// our stream2 will only emit a single JSON root node.
while (node = stream2.read()) {
console.log('got data: ', node);
}
});
}
示例8: HttpGetWithBasicAuth
function HttpGetWithBasicAuth() {
// using promises
needle('get', 'https://api.server.com', { username: 'you', password: 'secret' })
.then((resp) => {
// used HTTP auth
});
needle('get', 'https://username:password@api.server.com')
.then((resp) => {
// used HTTP auth from URL
});
// using callback
needle.get('https://api.server.com', { username: 'you', password: 'secret' }, (err, resp) => {
// used HTTP auth
});
needle.get('https://username:password@api.server.com', (err, resp) => {
// used HTTP auth from URL
});
}
示例9: API_get
function API_get() {
// using promises
needle('get', 'google.com/search?q=syd+barrett')
.then((resp) => {
// if no http:// is found, Needle will automagically prepend it.
});
// using callback
needle.get('google.com/search?q=syd+barrett', (err, resp) => {
// if no http:// is found, Needle will automagically prepend it.
});
}