Skip to content

Instantly share code, notes, and snippets.

@dtakahas
Last active March 28, 2016 23:04
Show Gist options
  • Save dtakahas/78f1cbd8cdc7714d9c62 to your computer and use it in GitHub Desktop.
Save dtakahas/78f1cbd8cdc7714d9c62 to your computer and use it in GitHub Desktop.
require "http/client"
require "json"
client = HTTP::Client.new "https://www.google.com", 443, true
response = client.get "/"
# Crystal 0.14.2
# crystal run request.cr
# Outputs:
# getaddrinfo: nodename nor servname provided, or not known (Socket::Error)
# [4413819154] *CallStack::unwind:Array(Pointer(Void)) +82
# [4413819057] *CallStack#initialize<CallStack>:Array(Pointer(Void)) +17
# [4413819016] *CallStack::new:CallStack +40
# [4413991737] *Socket::Error@Exception#initialize<Socket::Error, String, Nil>:CallStack +41
# [4413991668] *Socket::Error::new<String>:Socket::Error +100
# [4413985467] *TCPSocket#initialize<TCPSocket, String, Int32, Float64?, Float64?>:Nil +1835
# [4413983597] *TCPSocket::new<String, Int32, Float64?, Float64?>:TCPSocket +221
# [4413975630] *HTTP::Client#socket<HTTP::Client>:(OpenSSL::SSL::Socket | TCPSocket+) +110
# [4413975083] *HTTP::Client#exec_internal<HTTP::Client, HTTP::Request>:HTTP::Client::Response +43
# [4413975010] *HTTP::Client#exec<HTTP::Client, HTTP::Request>:HTTP::Client::Response +34
# [4413974477] *HTTP::Client#exec<HTTP::Client, String, String, Nil, Nil>:HTTP::Client::Response +29
# [4413974439] *HTTP::Client#get<HTTP::Client, String>:HTTP::Client::Response +39
# [4413780538] __crystal_main +37882
# [4413789720] main +40
# HTTP::Client.new does not accept a uri string, but a host only, so it should not have the protocol
require "http/client"
require "json"
client = HTTP::Client.new "www.google.com", 443, true
response = client.get "/"
@dtakahas
Copy link
Author

OS X 10.11.3

Dig:

$ dig 'https://www.google.com'

; <<>> DiG 9.8.3-P1 <<>> https://www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 27678
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;https://www.google.com.        IN  A

;; AUTHORITY SECTION:
google.com.     60  IN  SOA ns2.google.com. dns-admin.google.com. 118385882 900 900 1800 60

;; Query time: 32 msec
;; SERVER: 10.1.11.1#53(10.1.11.1)
;; WHEN: Mon Mar 28 13:38:14 2016
;; MSG SIZE  rcvd: 90

$ dig 'google.com'

; <<>> DiG 9.8.3-P1 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 35036
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4

;; QUESTION SECTION:
;google.com.            IN  A

;; ANSWER SECTION:
google.com.     300 IN  A   216.58.216.174

;; AUTHORITY SECTION:
google.com.     162532  IN  NS  ns3.google.com.
google.com.     162532  IN  NS  ns2.google.com.
google.com.     162532  IN  NS  ns1.google.com.
google.com.     162532  IN  NS  ns4.google.com.

;; ADDITIONAL SECTION:
ns1.google.com.     162532  IN  A   216.239.32.10
ns2.google.com.     162532  IN  A   216.239.34.10
ns3.google.com.     162532  IN  A   216.239.36.10
ns4.google.com.     162532  IN  A   216.239.38.10

;; Query time: 8 msec
;; SERVER: 10.1.11.1#53(10.1.11.1)
;; WHEN: Mon Mar 28 13:39:56 2016
;; MSG SIZE  rcvd: 180

@luislavena
Copy link

@dtakahas remove the protocol of HTTP::Client.new, is expecting host, port and ssl option, not URI:

https://gist.github.com/luislavena/72c43ed5d9b6096cc8de

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment