Pregunta

Dado el siguiente código ...

Net::HTTP.start('localhost', 4000) do |http|
    #
    #   usual stuff omitted for clarity
    #
    @response = http.request(req)
end

... si un (buen comportamiento) devuelve el servidor una respuesta 401 (no autorizado), ¿cómo puedo llegar a la cabecera WWW_Authenticate?

La mejor solución que tengo no es nada bueno ...

class Net::HTTPUnauthorized
    def get_header(h)
        _return = nil

        target = h.upcase

        self.header.each_header do |k, v|
            if k.upcase == target
                _return = v
                break
            end
        end

        _return
    end
end

Chris

¿Fue útil?

Solución

Una opción sería utilizar de halorgium rack-Client, que envuelve Net::HTTP con un estante punto final. A continuación, interactuar con el servidor remoto como si se tratara de una aplicación Rack:

response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top