I want to support all the native languages in the sub-domain names. For example, https://வணக்கம்blogs.example.co or niñaneverknew.example.co.
When I'm using this as வணக்கம்blogs
subdomain name. Browser is automatically converting it to punycode as xn--blogs-m5na4i9dyc2oc.
So I'm decoding it to unicode(xn--blogs-m5na4i9dyc2oc to வணக்கம்blogs) in our node server before fetching the data. And passing that also in the path of the https request.
const path = `/TakenSubdomains.json?orderBy="insensitiveName"&equalTo="${punycode.toUnicode(name))}"&limitToFirst=1&timeout=10s&auth=${firebaseAuthToken}`;
var options = {
method: "GET",
hostname: host,
path: path,
headers: {},
maxRedirects: 20,
};
return new Promise((resolve, reject) => {
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
const json = JSON.parse(body.toString());
resolve(json);
});
res.on("error", function (error) {
console.error(error);
reject(error);
});
});
req.end();
});
I got "request path contains unescaped characters" error.
So I used javascript inbuilt method called "encodeURIComponent" to escaping all the reserved characters.
const path = `/TakenSubdomains.json?orderBy="insensitiveName"&equalTo="${punycode.toUnicode(name))}"&limitToFirst=1&timeout=10s&auth=${firebaseAuthToken}`;
var options = {
method: "GET",
hostname: host,
path: encodeURIComponent(path),
headers: {},
maxRedirects: 20,
};
return new Promise((resolve, reject) => {
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
const json = JSON.parse(body.toString());
resolve(json);
});
res.on("error", function (error) {
console.error(error);
reject(error);
});
});
req.end();
});
Any help would be appreciated.
Thanks in advance.
CodePudding user response:
I used javascript inbuilt method called "encodeURIComponent" to escaping all the reserved characters.
But you used it on the whole path, not on the individual components. You should write
const path = `/TakenSubdomains.json?orderBy="insensitiveName"&equalTo="${encodeURIComponent(punycode.toUnicode(name))}"&limitToFirst=1&timeout=10s&auth=${encodeURIComponent(firebaseAuthToken)}`;
(Btw it's weird to see the query parameters wrapped in quotes, are those really necessary?)