Развернуть URL с помощью Ax ios? - PullRequest
0 голосов
/ 24 марта 2020

Я читал, как убрать URL с запроса в этой статье https://loige.co/unshorten-expand-short-urls-with-node-js/, и я хотел знать, можно ли это сделать в топоре ios.

Моя попытка:

 const geturl = async link => {
    const res = await axios({
      method: "get",
      url: link,
      maxRedirects: 1
    });
    console.log(res.headers.Location);
  };
geturl("URL GOES HERE");

Насколько я понимаю, Ax ios не использует расположение заголовков, как запрос ...

1 Ответ

2 голосов
/ 24 марта 2020

Вы можете установить maxRedirects на 0, тогда перенаправление будет считаться ошибкой, и вы сможете получить заголовок Location:

const geturl = async link => {
    try {
        return await axios({
            method: "get",
            url: link,
            maxRedirects: 0
        });
    } catch (e) {
        if (Math.trunc(e.response.status / 100) === 3) {
            console.log(e.response.headers.location);
            return geturl(e.response.headers.location);
        } else {
            throw e;
        }
    }
};

geturl("http://google.com").then(x => x.status).then(console.log);
...