如果我执行以下操作,则会跳过标题
require 'net/http'
require 'openssl'
require 'ap'
uri = URI("https://test:A234567@www.example.com/data")
http = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https')
request = Net::HTTP::Get.new uri
request['X-appname'] = 'testapp'
request['X-token'] = '1854fac3'
response = http.request request # Net::HTTPResponse object
ap response.body
如果我评论标题行,我会得到完全相同的错误,所以我说它们被跳过了。
错误是
"<Fault xmlns=\"http://schemas.microsoft.com/ws/2005/05/envelope/none\">
<Code><Value>Receiver</Value><Subcode><Value>NotAuthorized</Value>
</Subcode></Code><Reason><Text xml:lang=\"en-US\">Wrong username or
password.</Text></Reason></Fault>"
如果我在Bash做
curl -H 'X-appname: testapp' -H 'X-token: 1854fac3' https://test:A234567@www.example.com/data
然后它有效。
问题
任何人都可以看到为什么它不会被Ruby脚本弄错?
答案 0 :(得分:2)
看起来网址需要HTTP Basic Auth。您的案例中的错误与用户/密码相关 - Wrong username or password.
,与遗失的标题无关
您的代码中应该包含以下内容:
request.basic_auth 'test', 'A234567'
和URI应为
uri = URI("https://www.example.com/data")