Typhoid with cookies?

I need to get data from an API that authenticates with cookies. Here it works with curl.

curl -L -s -c cookie.txt "http://api.domain.com/login" -d username=" user@domain.com " -d password="password" > /dev/null curl -b cookie.txt "http://api.domain.com/user_data?param1=value1" > output.xml cat output.xml 

It works well

However, I cannot do the same using Typhoeus

I tried the following and it does not even authenticate.

 url = "http://api.domain.com/login" cookie_file_path = "cookie.txt" params = {param1: "value1"} body = {username: " user@domain.com ", 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 ) 

What am I missing?

 >>> 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"} 
+4
source share
1 answer

I would comment, but I can’t. It looks like you are getting a redirect. Try adding the followlocation: true parameter:

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

Source: https://habr.com/ru/post/1486149/


All Articles