-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
736 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* Example showing how one could do a simple request using the `Curl` wrapper. | ||
* | ||
* The `Curl` class is just a wrapper around the `Easy` class, it has all their methods, but instead | ||
* of being `sync`, it's `async`. This is achieved by using a `Multi` instance internally. | ||
*/ | ||
const { Curl, CurlFeature } = require('../dist') | ||
|
||
// by default all Curl instances set their user agent to `node-libcurl/${addonVersion}` | ||
// when they are created | ||
// you can change it using: | ||
Curl.defaultUserAgent = 'Something Else' | ||
// or simply remove it | ||
Curl.defaultUserAgent = null | ||
|
||
const curl = new Curl() | ||
|
||
// if you want to change the user agent for an already created instance you will need | ||
// to use their respective libcurl option, USERAGENT | ||
|
||
// can pass the url via argument to this example. node examples/01-simple-request.js http://www.example.com | ||
const url = process.argv[2] || 'http://www.google.com' | ||
|
||
// you can use the option name directly | ||
curl.setOpt('URL', url) | ||
|
||
// or use an already defined constant | ||
curl.setOpt(Curl.option.CONNECTTIMEOUT, 5) | ||
curl.setOpt(Curl.option.FOLLOWLOCATION, true) | ||
|
||
// Enable verbose mode | ||
curl.setOpt(Curl.option.VERBOSE, true) | ||
|
||
// If you use an invalid option, a TypeError exception will be thrown | ||
|
||
// By default, this can emit 4 events, `data`, `header`, `end` and `error`. | ||
// events are emitted with `this` bound to the handle, however if you | ||
// use an arrow function like below, you can still access the curl instance from the last argument | ||
|
||
// The `chunk` argument passed to the `data` and `header` events are raw Buffer objects. | ||
// The `body` argument passed to the `end` event is a string, which is the result of converting | ||
// all received data chunks to a utf8 string. | ||
// The `headers` one passed to the `end` event is an array of objects, it is an array because each redirect has their own | ||
// headers, in case there were no redirects in the request or the `FOLLOWLOCATION` option was | ||
// not used, the array will contain a single item. | ||
// You can disable this automatic conversion of the `body` and `headers` on the `end` event by enabling some features | ||
// on the `Curl` instance. For example: | ||
curl.enable(CurlFeature.NoDataParsing) | ||
curl.enable(CurlFeature.NoHeaderParsing) | ||
// or just: | ||
curl.enable(CurlFeature.Raw) | ||
|
||
// that way the `end` event will receive the raw `Buffer` objects for `data` and `headers`. | ||
// If you dont even want the `Curl` instance to store the data / headers, you can also enable features for that: | ||
curl.enable(CurlFeature.NoDataStorage) | ||
curl.enable(CurlFeature.NoHeaderStorage) | ||
// or just: | ||
curl.enable(CurlFeature.NoStorage) | ||
// NoStorage imples Raw | ||
|
||
// to get back to what it was before we can disable those features: | ||
curl.disable(CurlFeature.Raw | CurlFeature.NoStorage) | ||
|
||
curl.on('data', (chunk, curlInstance) => { | ||
console.log('Receiving data with size: ', chunk.length) | ||
}) | ||
|
||
curl.on('header', (chunk, curlInstance) => { | ||
console.log('Receiving headers with size: ', chunk.length) | ||
}) | ||
|
||
curl.on('end', (statusCode, body, headers, curlInstance) => { | ||
console.info('Status Code: ', statusCode) | ||
console.info('Headers: ', headers) | ||
console.info('Body length: ', body.length) | ||
|
||
// always close the `Curl` instance when you don't need it anymore | ||
// Keep in mind we can do multiple requests with the same `Curl` instance | ||
// before it's closed, we just need to set new options if needed | ||
// and call `.perform()` again. | ||
curl.close() | ||
}) | ||
|
||
// Error will be a JS error, errorCode will be the raw error code (as int) returned from libcurl | ||
curl.on('error', (error, errorCode) => { | ||
console.error('Error: ', error) | ||
console.error('Code: ', errorCode) | ||
curl.close() | ||
}) | ||
|
||
// this triggers the request | ||
curl.perform() | ||
|
||
// It's async, so it does not block the Node.js thread | ||
console.log('I will show before the request starts') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* Example showing how one could do a simple request using the `Easy` handle. | ||
*/ | ||
const { Curl, CurlCode, Easy } = require('../dist') | ||
|
||
const url = process.argv[2] || 'http://www.google.com' | ||
|
||
const handle = new Easy() | ||
|
||
// Just like before, we can use the option name, or the constant | ||
handle.setOpt('URL', url) | ||
handle.setOpt(Curl.option.VERBOSE, url) | ||
|
||
// This is used to receive the headers | ||
// See https://curl.haxx.se/libcurl/c/CURLOPT_HEADERFUNCTION.html | ||
handle.setOpt(Curl.option.HEADERFUNCTION, function(buf, size, nmemb) { | ||
console.log('HEADERFUNCTION: ') | ||
console.log(arguments) | ||
|
||
return size * nmemb | ||
}) | ||
|
||
// This is used to receive the headers | ||
// See https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html | ||
handle.setOpt(Curl.option.WRITEFUNCTION, function(buf, size, nmemb) { | ||
console.log('WRITEFUNCTION: ') | ||
console.log(arguments) | ||
|
||
return size * nmemb | ||
}) | ||
|
||
// this will trigger the request | ||
const ret = handle.perform() | ||
// The Easy handle will block the JS main thread: | ||
console.log('I will only show after the request has finished') | ||
|
||
// Remember to always close the handles | ||
handle.close() | ||
|
||
// In case there is something wrong, you can use Easy.strError to get a human readable string about the error | ||
console.log(ret, ret === CurlCode.CURLE_OK, Easy.strError(ret)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* Example showing how one could do a simple request using the `curlx` async fn | ||
*/ | ||
const querystring = require('querystring') | ||
|
||
const { curly } = require('../dist') | ||
|
||
const run = async () => { | ||
try { | ||
// there are many ways to use the curly.* functions | ||
let response = null | ||
|
||
// 1. Calling directly, which will default to a GET | ||
console.log('==================== REQUEST #1 ====================') | ||
response = await curly('http://www.httpbin.org/get') | ||
console.log(response.statusCode, response.headers, response.data) | ||
|
||
// 2. Using the curly.<http-verb> functions | ||
|
||
// get | ||
console.log('==================== REQUEST #2 ====================') | ||
response = await curly.get('http://www.httpbin.org/get') | ||
console.log(response.statusCode, response.headers, response.data) | ||
|
||
// post | ||
console.log('==================== REQUEST #3 ====================') | ||
const dataToSend = { | ||
//Data to send, inputName : value | ||
'input-arr[0]': 'input-arr-val0', | ||
'input-arr[1]': 'input-arr-val1', | ||
'input-arr[2]': 'input-arr-val2', | ||
'input-name': 'input-val', | ||
} | ||
response = await curly.post('http://httpbin.org/post', { | ||
postFields: querystring.stringify(dataToSend), | ||
// would work too: | ||
// POSTFIELDS: querystring.stringify(dataToSend), | ||
}) | ||
console.log(response.statusCode, response.headers, response.data) | ||
|
||
// 3 - Using writeFunction / headerFunction instead | ||
console.log('==================== REQUEST #4 ====================') | ||
response = await curly.get('http://httpbin.org/get', { | ||
writeFunction: (chunk, size, nmemb) => { | ||
// do something with chunk, which is a Buffer | ||
return size * nmemb | ||
}, | ||
headerFunction: (chunk, size, nmemb) => { | ||
// do something with chunk, which is a Buffer | ||
return size * nmemb | ||
}, | ||
}) | ||
// In this case headers will be an empty array and data will be a 0 length string | ||
console.log(response.statusCode, response.headers, response.data) | ||
|
||
// in case of errors, the libcurl error code will be inside `error.code` | ||
await curly.get('http://www.non-existing-domain.com') | ||
} catch (error) { | ||
console.error(`Error: ${error.message} - Code: ${error.code}`) | ||
} | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.