UNPKG

node_two_captcha_fork_emd

Version:

Node.js package for easy integration with 2Captcha API (Captcha Solver as a Service)

130 lines (102 loc) 3.59 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: http_request.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: http_request.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; const axios = require('axios'); const constants = require('./constants'); const querystring = require('querystring') /** * Class with static methods used in HTTP requests * @class */ class HTTPRequest { /** * Performs a GET to an URL and returns a promise to its body as a Buffer * * @param {string} url URL of the desired content to GET * @return {Promise&lt;Buffer>} Buffer with the content of the body from the HTTP response */ static async openDataURL(url) { if (typeof(url) !== 'string') throw new Error('You must inform a string URL'); const res = await axios.get(url, { responseType: 'arraybuffer' }); return res.data; } /** * Performs a request and returns a promise to the body as a string * * @param {Object} options Object with the parameters to the request * @param {string} options.url URL of the request * @param {string} [options.method='get'] HTTP verb of the request * @param {Object} [options.payload={}] Body content of the requisition * @param {number} [options.timeout=60000] Timeout of the request in miliseconds * @return {Promise&lt;string>} */ static async request(options = {}) { const url = options.url; const method = options.method || 'get'; const payload = options.payload || {}; const timeout = options.timeout || 60000; let headers = { 'User-Agent': constants.userAgent }; let res = await (async function() { switch (method) { case 'get': return await axios.get(`${url}?${querystring.stringify(payload)}`, { headers: headers, timeout: timeout }); break; case 'post': return await axios.post(url, querystring.stringify(payload), { headers: headers, timeout: timeout }); break; case 'multipart': headers['content-type'] = 'multipart/form-data'; return await axios.post(url, querystring.stringify(payload), { headers: headers, timeout: timeout }); break; default: throw new Error(`Illegal HTTP method (${method})`); } })(); return res.data; } } module.exports = HTTPRequest; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Captcha.html">Captcha</a></li><li><a href="HTTPRequest.html">HTTPRequest</a></li><li><a href="TwoCaptchaClient.html">TwoCaptchaClient</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Mon May 17 2021 19:14:49 GMT-0300 (Brasilia Standard Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>