Net :: HTTP :: non autorizzata - come faccio a ottenere l'intestazione WWW-Authenticate?

StackOverflow https://stackoverflow.com/questions/2599126

  •  25-09-2019
  •  | 
  •  

Domanda

Dato il codice qui sotto ...

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

... se un (ben educati) server restituisce un (non autorizzata) di risposta 401, come faccio arrivare alla WWW_Authenticate intestazione?

La soluzione migliore che ho non è affatto buona ...

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

È stato utile?

Soluzione

Una possibilità sarebbe quella di utilizzare di halorgium Rack-client, che avvolge Net::HTTP con un rack endpoint. Si potrebbe quindi interagire con il server remoto come se fosse un'applicazione Rack:

response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top