vapor/http

follow redirects

sergeydi opened this issue · 3 comments

I need to get result of:

$ curl ipinfo.io/8.8.8.8
{
  "ip": "8.8.8.8",
  "hostname": "google-public-dns-a.google.com",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3860,-122.0840",
  "postal": "94035",
  "phone": "650",
  "org": "AS15169 Google LLC"
}

My code:

let eventLoopGroup = MultiThreadedEventLoopGroup(numThreads: 1)
let eventLoop = eventLoopGroup.next()
let client = try! HTTPClient.connect(hostname: "ipinfo.io", on: eventLoop).wait()
let httpReq = HTTPRequest(method: .GET, url: "/8.8.8.8")
let httpRes = try! client.send(httpReq).wait()
print(httpRes)

I received:

Date: Thu, 07 Jun 2018 10:10:34 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 47
X-Powered-By: Express
x-cloud-trace-context: e942dcd1f79372a1b17f146831abb7df/9141077538359615180;o=0
Access-Control-Allow-Origin: *
Location: https://ipinfo.io/8.8.8.8
Vary: Accept
Via: 1.1 google
Found. Redirecting to https://ipinfo.io/8.8.8.8

What I am doing wrong?

vzsg commented

The server returned a redirect to https://ipinfo.io/8.8.8.8 which HTTPClient didn't even try to follow. I don't know if it's supposed to do so currently. If so: you ran into a bug; or if not, this would be a useful feature to have.

I found additional option for HTTPClient.connect:

let client = try! HTTPClient.connect(scheme: .https, hostname: "ipinfo.io", on: eventLoop).wait()

But during execution of the code, I got:

Fatal error: 'try!' expression unexpectedly raised an error: NIOOpenSSL.OpenSSLError.uncleanShutdown: file /home/buildnode/jenkins/workspace/oss-swift-4.1-package-linux-ubuntu-16_04/swift/stdlib/public/core/ErrorType.swift, line 184

My system:

$ cat /etc/lsb-release 
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
$ swift --version
Swift version 4.1.2 (swift-4.1.2-RELEASE)
Target: x86_64-unknown-linux-gnu

Leaving this open as a feature request for following redirects.