从技术上讲,您可以使用 before_execution_proc 参数来实现它,它允许您访问内部 Net::HTTP 请求对象。如果您使用的是 ruby-ntlm gem,它会向 Net::HTTP 请求添加一个 ntlm_auth 方法。
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