2014-02-19 3 views
0

RestClient를 사용하여 게시물에 사용자 이름과 암호를 보내려면 어떻게해야합니까? 내 클라이언트 코드는 다음과 같습니다 :RestClient를 사용하여 사용자 이름과 암호를 보내는 방법

response = RestClient.post 'http://localhost:3000/api/rules', 
    :name => "me", :password => "12345", 
    :books => {:book_name => "Harry Porter", 
      :author => "JR"} 

서버 코드가 레일에와`http_basic_authenticate_with 사용한다 : 이름 => "나": 암호 => "12345"를.

당신은 URL 문자열에 사용자 이름과 암호를 추가 할 수 있습니다

답변

0

,

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"}) 
관련 문제