2013-06-14 3 views
4

쿠키로 인증하는 API에서 데이터를 가져와야합니다. 여기가 말풍선과 함께 작동합니다.쿠키가있는 Typhoeus?

curl -L -s -c cookie.txt "http://api.domain.com/login" -d username="[email protected]" -d password="password" > /dev/null 

curl -b cookie.txt "http://api.domain.com/user_data?param1=value1" > output.xml 

cat output.xml 

위의 작품은 잘

그러나 나는 다음과도 인증하지 않습니다

Typhoeus

를 내가 시도를 사용하여 같은 일을 할 수없는입니다.

url = "http://api.domain.com/login" 
cookie_file_path = "cookie.txt" 
params = {param1: "value1"} 
body = {username: "[email protected]", password: "password"} 

request = Typhoeus::Request.new(
    url, 
    method: :post, 
    params: params, 
    body: body, 
    cookiejar: cookie_file_path, 
    #headers: {Accept: "text/html"}, 
    verbose: true 
) 

response = request.run 
cookies = CookieJar.new.parse(response.headers_hash["Set-Cookie"]) 

puts cookies # doesn't show me anything. 

url = "http://api.domain.com/user_data" 
cookie_file_path = "cookie.txt" 
params = {output: "xml"} 
body = {} 

request = Typhoeus::Request.new(
    url, 
    method:  :get, 
    params:  params, 
    body:  body, 
    cookiejar: cookie_file_path, 
    cookiefile: cookie_file_path, 
    #headers:  {Accept: "text/html"}, 
    verbose: true 
) 

무엇이 누락 되었습니까?

>>> response.code 
302 
>>> response.headers_hash 
{"Server"=>"squid/3.1.10", "Mime-Version"=>"1.0", "Date"=>"Fri, 14 Jun 2013 02:38:28 GMT", "Content-Type"=>"text/html", "Content-Length"=>"3830", "X-Squid-Error"=>"ERR_INVALID_REQ 0", "Vary"=>"Accept-Language", "Content-Language"=>"en", "X-Cache"=>"MISS from domain.com", "X-Cache-Lookup"=>"NONE from domain.com:3128", "Via"=>"1.0 domain.com (squid/3.1.10)", "Connection"=>"close"} 
+0

헤더 해시에 'Set-Cookie'가 전혀 표시되지 않습니다. –

+0

안녕하세요, 해결책을 찾으셨습니까? – zeemy23

답변

0

나는 말할 수도 있지만 할 수 없다. 리디렉션을 받고있는 것 같습니다. followlocation: true 옵션을 추가해보세요.

request = Typhoeus::Request.new(
    url, 
    followlocation: true, # <-- 
    method: :post, 
    params: params, 
    body: body, 
    cookiejar: cookie_file_path, 
    #headers: {Accept: "text/html"}, 
    verbose: true 
) 
관련 문제