1

Something I'm running into when using Vapor to crawl sites is if I enter a url like http://website(dot)com and it redirects to https://www.website(dot)com, I need to grab that final response URL to know what the final page URL is.

I can do this with URLSession but I'm not seeing something similar in the Vapor client. I tried using the Vapor client and the async-http package it's built on and I can't find the page URL listed in the response.

Any pointers would be appreciated, thank you.

Example getting this info using URLSession

if let url = URL(string: self) {
            do {
                let(_, response) = try await URLSession.shared.data(from: url)
                if let responseURL = response.url {
                    result = responseURL.description
                    return result
                }
            } catch {
            }
        }

Where this is the first line of the response, and you can easily use response.url to grab the below "URL" value:

<NSHTTPURLResponse: 0x600000210c00> { URL: https://github.com/ } { Status Code: 200, Headers {

Graham
  • 19
  • 2
  • There is a thread on the Swift Forums with a workable solution for this question: https://forums.swift.org/t/vapor-how-to-get-response-url-using-async-http-vapor-client/62418 Please post your solution once you finish the code. – John Hanley Jan 21 '23 at 04:01
  • Show what you have tried in vapor. – Nick Feb 21 '23 at 06:29

0 Answers0