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


TypeScript needle類代碼示例

本文整理匯總了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: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

示例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;未經允許,請勿轉載。