0

How can I get only response headers in an em_http_request?

I tried to use this code:

EventMachine.run do
  http = EventMachine::HttpRequest.new('my_url').get
  http.headers do |headers|
    Fiber.current.resume headers
  end
end

but I don't want to receive the whole body. How I can stop the request's execution? http.close doesn't work.

UPD
http.instance_variable_get(:'@conn').close helps me, but may be you know more interesting solution

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Falcon
  • 1,461
  • 3
  • 15
  • 29

1 Answers1

1

If you don't want the body, you should do a HEAD request instead of a GET. To terminate the event loop you need to explicitly call EventMachine.stop.

EventMachine.run do
  http = EventMachine::HttpRequest.new('my_url').head
  http.headers do |headers|
    # do something with headers

    EventMachine.stop
  end
end
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Not the event loop. Just request. See UPD – Falcon Oct 24 '14 at 15:51
  • +1, yes, a HEAD request is the proper way to do this. – the Tin Man Oct 24 '14 at 16:15
  • When you do a `HEAD` request, the request is over after the headers were received _by definition_. I suppose that by the time the `header` callback is called, the connection may already be closed. If you don't want to terminate the event loop, just remove the `EventMachine.stop` – Patrick Oscity Oct 24 '14 at 16:36