Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix: use encodeURIComponent instead of encodeURI (#1198)
Szymon Marczakcd98da0dce17
diff --git a/src/utils_request.js b/src/utils_request.jsindex 273864910..3561d16ef 100644--- a/src/utils_request.js+++ b/src/utils_request.js@@ -227,6 +227,7 @@ export const requestAsBrowser = async (options = {}) => {* @example fixUrl('https://example.com/%xx') => 'https://example.com/%25xx'* @example fixUrl('https://example.com/%0fexample%cc%0f') => 'https://example.com/%0Fexample%EF%BF%BD%0F'* @see https://tc39.es/ecma262/multipage/global-object.html#sec-decodeuri-encodeduri+ * @see https://en.wikipedia.org/wiki/Percent-encoding* @param {string} url* @private*/@@ -254,7 +255,7 @@ function fixUrl(url) {} while (url[index] === '%');if (chunks.length) {- const encoded = encodeURI(Buffer.from(chunks, 'hex').toString());+ const encoded = encodeURIComponent(Buffer.from(chunks, 'hex').toString());url = `${url.slice(0, start)}${encoded}${url.slice(index)}`;index = start + encoded.length;diff --git a/test/utils_request.test.js b/test/utils_request.test.jsindex 6ca0c54cd..93692e149 100644--- a/test/utils_request.test.js+++ b/test/utils_request.test.js@@ -42,7 +42,10 @@ describe('Apify.utils_request', () => {});app.get('/test-encoding', (req, res) => {- res.json(req.query);+ res.json({+ parsed: req.query,+ url: req.url,+ });});app.get('/invalidBody', async (req, res) => {@@ -104,20 +107,25 @@ describe('Apify.utils_request', () => {});const testQueries = [- ['abc', 'abc'],- ['%20', ' '],- ['%cf', '�'],- ['helios-–-the-primordial-sun', 'helios-–-the-primordial-sun'],- ['helios-%E2%80%93-the-primordial-sun', 'helios-–-the-primordial-sun'],- ['%C3%A8----%C3%A9', 'è----é'],+ // [query, decoded, raw]+ ['abc', 'abc', 'abc'],+ ['%20', ' ', '%20'],+ ['%cf', '�', '%EF%BF%BD'],+ ['helios-–-the-primordial-sun', 'helios-–-the-primordial-sun', 'helios-%E2%80%93-the-primordial-sun'],+ ['helios-%E2%80%93-the-primordial-sun', 'helios-–-the-primordial-sun', 'helios-%E2%80%93-the-primordial-sun'],+ ['%C3%A8----%C3%A9', 'è----é', '%C3%A8----%C3%A9'],];- test.each(testQueries)(`it works with not encoded urls: '%s' (regular)`, async (query, decoded) => {+ test.each(testQueries)(`it works with not encoded urls: '%s' (regular)`, async (query, decoded, raw) => {const response = await requestAsBrowser({url: `http://${HOSTNAME}:${port}/test-encoding/?q=${query}`,});expect(response.statusCode).toBe(200);- expect(JSON.parse(response.body).q).toBe(decoded);++ const result = JSON.parse(response.body);++ expect(result.parsed.q).toBe(decoded);+ expect(result.url).toBe(`/test-encoding/?q=${raw}`);});test.each(testQueries)(`it works with not encoded urls: '%s' (stream)`, async (query, decoded) => {@@ -133,7 +141,7 @@ describe('Apify.utils_request', () => {const body = JSON.parse(chunks.join());expect(response.statusCode).toBe(200);- expect(body.q).toBe(decoded);+ expect(body.parsed.q).toBe(decoded);});test('uses desktop user-agent by default ', async () => {