如何使用RestClient在帖子中发送用户名和密码?我的客户代码如下:
response = RestClient.post 'http://localhost:3000/api/rules',
:name => "me", :password => "12345",
:books => {:book_name => "Harry Porter",
:author => "JR"}
服务器代码在Rails中,并使用`http_basic_authenticate_with:name =>“ me”,:password =>“ 12345”。
您可以将用户名和密码添加到URL字符串中,
username = 'me'
password = '12345'
response = RestClient.post "http://#{username}:#{password}@localhost:3000/api/rules",
:books => {:book_name => "Harry Porter", :author => "JR"}
或者您可以像这样将它们传递给哈希,
resource = RestClient::Resource.new('http://localhost:3000/api/rules', :user => 'me', :password => '12345')
response = resource.post(:books => {:book_name => "Harry Porter", :author => "JR"})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句