Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP::Client#exec causing some "400 Bad Request" response when using it with HTTP::Request object. #6480

Closed
newtonapple opened this issue Aug 4, 2018 · 0 comments

Comments

@newtonapple
Copy link

The following code triggers a 400 response from example.com:

require "http/client"

client = HTTP::Client.new "www.example.com"
req = HTTP::Request.new("GET", "/")
resp = client.exec HTTP::Request.new("GET", "/")
puts resp.status_code # 400

Note that HTTP::Client#exec seems to be generating an HTTP request without the Host header. Adding the missing header will trigger expected 200 response.

req2 = HTTP::Request.new("GET", "/", HTTP::Headers.new.tap{|h| h["Host"] = "example.com"})
resp2 = client.exec req2
puts resp2.status_code # 200

Class method is working as expected:

puts HTTP::Client.exec("GET", "http://www.example.com/").status_code # 200

I also noticed that this didn't seem to be a problem on google.com

client2 = HTTP::Client.new "www.google.com"
resp3 = client2.exec(req)
puts resp3.status_code # 200

The reason I ran into this problem was because I was trying to send the same Request object to multiple Hosts. I expect I should be able to reuse the same request object cross multiple client / hosts. Is this an acceptable usage pattern of the HTTP::Client API?

crystal -v
Crystal 0.25.1 (2018-06-30)

LLVM: 5.0.2
Default target: x86_64-apple-macosx
@newtonapple newtonapple changed the title HTTP::Client#exec causing some "400 Bad Request" response when using it HTTP::Request object. HTTP::Client#exec causing some "400 Bad Request" response when using it with HTTP::Request object. Aug 4, 2018
@RX14 RX14 closed this as completed in #6481 Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant