I can't figure out how to follow redirects from an external URL. The URL I am accessing is for some reason giving me 301, even though I don't get it in postman. I am wondering how I can get HTTPClient to follow the redirect or if I am doing anything wrong in order to get a 301.
I did read this PR, but I don't think it is relevant anymore. Nor does the HTTPClient documentation cover redirects. In the example below I've set the User-Agent to the same as Postman, in order to mimic Postman's request as much as possible.
My code so far:
import Vapor
import HTTP
protocol DataFetching {
func getFromURL(_ url: URL, on worker: Worker) throws -> Future<HTTPResponse>
}
class DataFetcher: DataFetching {
enum Error: Swift.Error {
case unknown
}
func getFromURL(_ url: URL, on worker: Worker) throws -> Future<HTTPResponse> {
guard let host = url.host else { throw Error.unknown }
var headers = HTTPHeaders()
headers.replaceOrAdd(name: .userAgent, value: "PostmanRuntime/7.4.0")
headers.replaceOrAdd(name: .accept, value: "*/*")
headers.replaceOrAdd(name: .cacheControl, value: "no-cache")
headers.replaceOrAdd(name: .applyToRedirectRef, value: "true")
headers.replaceOrAdd(name: .acceptEncoding, value: "gzip, deflate")
let httpRequest = HTTPRequest(method: .GET, url: url, version: HTTPVersion(major: 1, minor: 1), headers: headers)
return HTTPClient.connect(hostname: host, on: worker).flatMap { client in
return client.send(httpRequest)
}
}
}
What am I missing?