Net :: HTTP :: non autorisée - comment puis-je l'en-tête WWW-Authenticate?
-
25-09-2019 - |
Question
Vu le code ci-dessous ...
Net::HTTP.start('localhost', 4000) do |http|
#
# usual stuff omitted for clarity
#
@response = http.request(req)
end
... si un serveur (bien élevé) renvoie une réponse 401 (non autorisée), comment puis-je l'en-tête de WWW_Authenticate?
La meilleure solution que j'ai est pas du tout bon ...
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
La solution
Une option serait d'utiliser rack client de halorgium, qui enveloppe Net::HTTP
avec un rack point final. Vous pouvez ensuite interagir avec le serveur distant comme si elle était une application Rack:
response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow