1

I'm trying to parse a URL and get its components like protocol,hostname,port and I guess the URL class is made for this.
But there's one thing I do not understand:
Parsing a URL like 'ftps://example.com:22' is not working.
Here's a simple test
url = new URL("ftps://example.com:22");
console.log(url);

and that's what I get in the console

[object URL] {
hash: "",
host: "",
hostname: "",
href: "ftps://example.com:22",
origin: "null",
password: "",
pathname: "//example.com:22",
port: "",
protocol: "ftps:",
...

As can be seen the 'hostname' and even the 'port' are empty.
If I do the same test with http://example.com:22 every thing is ok, 'hostname' and 'port' have the correct values.

Here's a fiddle I tested it with

What am I doing wrong? Is the URL class not the right thing to use?
Many thanks in advance for any enlightenment

Chris
  • 697
  • 4
  • 17

2 Answers2

1

By looking at the URL Standard URL representation table of URL's scheme / host combinations, "ftps" scheme is not allowed. However "ftp" is allowed so you may use that.

I also tried parsing the URL using the a element as described in an anwser to How do I parse a URL into hostname and path in javascript?, but it is the same. "ftps" does not work, but "ftp" does.

So the easisest way is probably to parse it with "ftp" and get the values you want, or do the parsing manually.

touchmarine
  • 1,548
  • 9
  • 11
0

I think the URL constructor only recognises ftp:// URLs and not ftps://

charlieb
  • 59
  • 3