Get only response headers in em_http_request

106 views Asked by At

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

1

There are 1 answers

3
Patrick Oscity On

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