本文整理汇总了TypeScript中needle类的典型用法代码示例。如果您正苦于以下问题:TypeScript needle类的具体用法?TypeScript needle怎么用?TypeScript needle使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了needle类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。
示例1: API_request
function API_request() {
const params = {
q: 'a very smart query',
page: 2,
};
// using promises
needle('get', 'forum.com/search', params)
.then((resp) => {
if (resp.statusCode === 200)
console.log(resp.body); // here you go, mister.
});
needle('get', 'forum.com/search', params, { json: true })
.then((resp) => {
if (resp.statusCode === 200) console.log('It worked!');
});
// using callback
needle.request('get', 'forum.com/search', params, (err, resp) => {
if (!err && resp.statusCode === 200)
console.log(resp.body); // here you go, mister.
});
needle.request('get', 'forum.com/search', params, { json: true }, (err, resp) => {
if (resp.statusCode === 200) console.log('It worked!');
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:28,代码来源:needle-tests.ts
示例2: Multipart
function Multipart() {
const buffer = fs.readFileSync('/path/to/package.zip');
const data = {
zip_file: {
buffer,
filename: 'mypackage.zip',
content_type: 'application/octet-stream'
}
};
// using promises
needle('post', 'http://somewhere.com/over/the/rainbow', data, { multipart: true })
.then((resp) => {
// if you see, when using buffers we need to pass the filename for the multipart body.
// you can also pass a filename when using the file path method, in case you want to override
// the default filename to be received on the other end.
});
// using callback
needle.post('http://somewhere.com/over/the/rainbow', data, { multipart: true }, (err, resp, body) => {
// if you see, when using buffers we need to pass the filename for the multipart body.
// you can also pass a filename when using the file path method, in case you want to override
// the default filename to be received on the other end.
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:26,代码来源:needle-tests.ts
示例3: 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);
}
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:44,代码来源:needle-tests.ts
示例4: HttpGetWithBasicAuth
function HttpGetWithBasicAuth() {
// using promises
needle('get', 'https://api.server.com', { username: 'you', password: 'secret' })
.then((resp) => {
// used HTTP auth
});
needle('get', 'https://username:[email protected]')
.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:[email protected]', (err, resp) => {
// used HTTP auth from URL
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:19,代码来源:needle-tests.ts
示例5: 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.
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:12,代码来源:needle-tests.ts
示例6: DigestAuth
function DigestAuth() {
// using promises
needle('get', 'other.server.com', { username: 'you', password: 'secret', auth: 'digest' })
.then((resp) => {
// needle prepends 'http://' to your URL, if missing
});
// using callback
needle.get('other.server.com', { username: 'you', password: 'secret', auth: 'digest' }, (err, resp, body) => {
// needle prepends 'http://' to your URL, if missing
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:12,代码来源:needle-tests.ts
示例7: API_head
function API_head() {
// using promises
needle('head', 'https://my.backend.server.com')
.then((resp) => console.log('Yup, still alive.'))
.catch((err: Error) => console.log('Shoot! Something is wrong: ' + err.message));
// using callback
needle.head('https://my.backend.server.com', (err, resp) => {
if (err) {
console.log('Shoot! Something is wrong: ' + err.message);
} else {
console.log('Yup, still alive.');
}
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:15,代码来源:needle-tests.ts
示例8: 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);
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:15,代码来源:needle-tests.ts
示例9: FileUpload
function FileUpload() {
const data = {
foo: 'bar',
image: { file: '/home/tomas/linux.png', content_type: 'image/png' }
};
// using promises
needle('post', 'http://my.other.app.com', data, { multipart: true })
.then((resp) => {
// needle will read the file and include it in the form-data as binary
});
needle('put', 'https://api.app.com/v2', fs.createReadStream('myfile.txt'))
.then((resp) => {
// stream content is uploaded verbatim
});
// using callback
needle.post('http://my.other.app.com', data, { multipart: true }, (err, resp, body) => {
// needle will read the file and include it in the form-data as binary
});
needle.put('https://api.app.com/v2', fs.createReadStream('myfile.txt'), (err, resp, body) => {
// stream content is uploaded verbatim
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:24,代码来源:needle-tests.ts
示例10: API_post
function API_post() {
const options = {
headers: { 'X-Custom-Header': 'Bumbaway atuna' }
};
// using promises
needle('post', 'https://my.app.com/endpoint', 'foo=bar', options)
.then((resp) => {
// you can pass params as a string or as an object.
});
// using callback
needle.post('https://my.app.com/endpoint', 'foo=bar', options, (err, resp) => {
// you can pass params as a string or as an object.
});
}
开发者ID:AbraaoAlves,项目名称:DefinitelyTyped,代码行数:16,代码来源:needle-tests.ts
注:本文中的needle类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论