Request path contents unescaped characters error when accessing nodejs site

Asked 2 years ago, Updated 2 years ago, 44 views

This article is a kind of stuffed text that you write in anger after experiencing a situation in which the original text is deleted while writing an answer. ๐Ÿ˜’

var client = require('cheerio-httpcli');
let request = require('request');
constjson = '{"portal":"next",title":"drinking"}' //input data
const obj = JSON.parse(json);
console.log('obj.title: [' + obj.title + ']');
var url = 'http://webtoon.daum.net/data/pc/search?q=' + obj.title + '&page_no=1';

client.fetch(url, {}, function (err, $, res, body) {
    console.log('err:', err);
    console.log('$:', $);
});

const getArticlesTitle = (url) => {
    request({ url: url }, function (error, response, body) {
        console.log('body:', body);
    });
};

getArticlesTitle(url);

If you run the above code as nodejs:

TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
    at new ClientRequest (_http_client.js:139:13)
    at Object.request (http.js:44:10)
    at Request.start (C:\dev\cheerio-test\node_modules\request\request.js:751:32)
    at Request.end (C:\dev\cheerio-test\node_modules\request\request.js:1511:10)
    at end (C:\dev\cheerio-test\node_modules\request\request.js:564:14)
    at Immediate._onImmediate (C:\dev\cheerio-test\node_modules\request\request.js:578:7)
    at processImmediate (internal/timers.js:439:21) {
  url: 'http://webtoon.daum.net/data/pc/search?q=Eating&page_no=1'
}

This error occurs. What's wrong with this?

node.js javascript

2022-09-21 15:55

1 Answers

Replace url's Korean:

var url = 'http://webtoon.daum.net/data/pc/search?q=' 
        + + encodeURI(obj.title)
        + + '&page_no=1';


2022-09-21 15:55

If you have any answers or tips


ยฉ 2024 OneMinuteCode. All rights reserved.