Can Rest Client do NTLM authentication?
I didn't se开发者_运维问答e any options in the documentation for authentication types:
require 'rest_client'
resource = RestClient::Resource.new 'http://website', :auth_type => 'ntlm', :user => 'USERNAME', :password => 'PASSWORD'
results = resource.get
:auth_type => 'ntlm'
doesn't work, and I couldn't find anything on the documentation or IRC room either.
The NTLM requirement really narrows down what HTTP software you can use due to it being so specific to Microsoft.
You might want to look at "NTLM Authentication for Ruby with Typhoeus and Curl", then look into using Typhoeus instead of rest-client.
Technically speaking, you can make it do so using the before_execution_proc arg which lets you access the internal Net::HTTP request objects. If you're using the ruby-ntlm gem it adds a ntlm_auth method to Net::HTTP requests.
require 'ntlm/http'
require 'rest-client'
require 'json'
# Quick monkey patch to rest client payloads since for some reason Net/NTLM insists on playing payload streams backwards.
class RestClient::Payload::Base
def rewind
@stream.rewind
end
end
auth_proc = ->(req, _args){ req.ntlm_auth(username, domain, password)}
res = RestClient::Request.new(method: :post, url: url, payload: payload}, before_execution_proc: auth_proc ).execute
res
精彩评论