當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript needle.get函數代碼示例

本文整理匯總了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());
        }
    });
}
開發者ID:DavidKDeutsch,項目名稱:DefinitelyTyped,代碼行數:25,代碼來源:needle-tests.ts

示例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!');
    })
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:28,代碼來源:needle-tests.ts

示例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
    });
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:8,代碼來源:needle-tests.ts

示例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);
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:11,代碼來源:needle-tests.ts

示例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);
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:15,代碼來源:needle-tests.ts

示例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);
        });
    });
開發者ID:eastenluis,項目名稱:wechat-subscription,代碼行數:33,代碼來源:wechat-subscription-account.ts

示例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);
        }
    });
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:44,代碼來源:needle-tests.ts

示例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
    });
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:19,代碼來源:needle-tests.ts

示例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.
    });
}
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:12,代碼來源:needle-tests.ts


注:本文中的needle.get函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。