简体   繁体   中英

getting the original url from http response

I have this function that reads a csv file that contains a list of url. For every line I do a get request to the current url to read the response header.

function readCSV(csv) {
  var lines = csv.split("\n");
  var table = lines.map((line) => line.split(","));
  var requests = table.map((row) =>
    request({
      method: "GET",
      uri: "https://www." + row[1],
      resolveWithFullResponse: true,
    })
    .catch((err) => null) // Errors are ignored and resolved as `null`
  );

  return Promise.all(requests)
    .then((responses) => {
    
      responses.forEach((response) => {
        if(response === null) return; // If the response is null, skip it

        // ... handle successful responses here
        var hrds = response.headers;
        //console.log(hrds.url)
        console.log(hrds['content-security-policy'])
        console.log(hrds['x-frame-options'])

      });
    })
    .catch((err) => console.log(err));
}

I would like to do something like: console.log(hrds.url) to see the url that sent me the response. If I try to print hrds.url it gives me undefined.

EDIT: I tried response.url but it prints blank line

According to MDN docs you can get the URL from the response it self.

response.url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM