Net :: http :: غير مصرح به - كيف يمكنني الوصول إلى رأس www -authenticate؟

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

  •  25-09-2019
  •  | 
  •  

سؤال

بالنظر إلى الرمز أدناه ...

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

... إذا قام خادم (بتصرف جيد) بإرجاع استجابة 401 (غير مصرح بها) ، فكيف يمكنني الحصول على رأس www_authenticate؟

أفضل حل لدي ليس جيدًا على الإطلاق ...

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

كريس

هل كانت مفيدة؟

المحلول

خيار واحد هو الاستخدام رف هالورجيوم العميل, التي تلتف Net::HTTP مع نقطة نهاية الرف. ستتفاعل بعد ذلك مع الخادم البعيد كما لو كان تطبيق رف:

response = Rack::Client.get("http://localhost:4000/foo/bar.baz")
response.code
# => 401
response.headers['WWW-Authenticate']
# => 'Basic realm="Control Panel"'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top