当前位置: 动力学知识库 > 问答 > 编程问答 >

Ruby output raw HTTP request

问题描述:

A script of mine submits an HTTP request, but I'd like to send the raw HTTP request to an associate for troubleshooting. Is there a way I can output the raw HTTP request to a file?

网友答案:

If you're using Net::HTTP, use #set_debug_output:

1.9.3p194 :017 > http = Net::HTTP.new('google.com')
 => #<Net::HTTP google.com:80 open=false> 
1.9.3p194 :018 > http.request_get('/')
 => #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true> 
1.9.3p194 :019 > http.set_debug_output($stdout)
 => #<IO:<STDOUT>> 
1.9.3p194 :020 > http.request_get('/')
opening connection to google.com...
opened
<- "GET / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Ruby\r\nConnection: close\r\nHost: google.com\r\n\r\n"
-> "HTTP/1.1 301 Moved Permanently\r\n"
-> "Location: http://www.google.com/\r\n"
-> "Content-Type: text/html; charset=UTF-8\r\n"
-> "Date: Mon, 23 Jul 2012 21:41:13 GMT\r\n"
-> "Expires: Wed, 22 Aug 2012 21:41:13 GMT\r\n"
-> "Cache-Control: public, max-age=2592000\r\n"
-> "Server: gws\r\n"
-> "Content-Length: 219\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading 219 bytes...
-> "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n"
read 219 bytes
Conn close
 => #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true> 

Or with HTTParty, use the :debug_output option:

1.9.3p194 :028 > HTTParty.get('http://www.google.com', :debug_output => $stdout)
opening connection to www.google.com...
opened
<- "GET / HTTP/1.1\r\nConnection: close\r\nHost: www.google.com\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "Date: Mon, 23 Jul 2012 21:42:55 GMT\r\n"
-> "Expires: -1\r\n"
-> "Cache-Control: private, max-age=0\r\n"
-> "Content-Type: text/html; charset=ISO-8859-1\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=www.google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.www.google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=google.com\r\n"
-> "Set-Cookie: expires=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: path=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: domain=; expires=Mon, 01-Jan-1990 00:00:00 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: PREF=ID=bf7ea711fb78b88f:FF=0:TM=1343079775:LM=1343079775:S=sitzmIr74uTXkzTt; expires=Wed, 23-Jul-2014 21:42:55 GMT; path=/; domain=.google.com\r\n"
-> "Set-Cookie: NID=62=i-gpt9kPmB80W787ganAdeSSc502sPHOT5bwpddGHTbVtAB5-oDjjaJtQAN7znbEk70RkuHDui5sJOlZrXB0MzfIKtpU2x7vRDj3vFQch6BRcqQaVmzm_J7OzAIOpB5H; expires=Tue, 22-Jan-2013 21:42:55 GMT; path=/; domain=.google.com; HttpOnly\r\n"
-> "P3P: CP=\"This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info.\"\r\n"
-> "Server: gws\r\n"
-> "X-XSS-Protection: 1; mode=block\r\n"
-> "X-Frame-Options: SAMEORIGIN\r\n"
-> "Connection: close\r\n"
-> "\r\n"
reading all...
# ... snip ...

At this point, you can copy that output, or you can replace $stdout with any IO instance, like a File:

1.9.3p194 :034 > File.open('/tmp/http.out', 'w') {|f| http.set_debug_output(f); http.get('/')}
 => #<Net::HTTPMovedPermanently 301 Moved Permanently readbody=true> 
1.9.3p194 :035 > File.read('/tmp/http.out')
 => "opening connection to google.com...\nopened\n<- "GET / HTTP/1.1\\r\\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\\r\\nAccept: */*\\r\\nUser-Agent: Ruby\\r\\nConnection: close\\r\\nHost: google.com\\r\\n\\r\\n"\n-> "HTTP/1.1 301 Moved Permanently\\r\\n"\n-> "Location: http://www.google.com/\\r\\n"\n-> "Content-Type: text/html; charset=UTF-8\\r\\n"\n-> "Date: Mon, 23 Jul 2012 21:46:02 GMT\\r\\n"\n-> "Expires: Wed, 22 Aug 2012 21:46:02 GMT\\r\\n"\n-> "Cache-Control: public, max-age=2592000\\r\\n"\n-> "Server: gws\\r\\n"\n-> "Content-Length: 219\\r\\n"\n-> "X-XSS-Protection: 1; mode=block\\r\\n"\n-> "X-Frame-Options: SAMEORIGIN\\r\\n"\n-> "Connection: close\\r\\n"\n-> "\\r\\n"\nreading 219 bytes...\n-> "<HTML><HEAD><meta http-equiv=\\"content-type\\" content=\\"text/html;charset=utf-8\\">\\n<TITLE>301 Moved</TITLE></HEAD><BODY>\\n<H1>301 Moved</H1>\\nThe document has moved\\n<A HREF=\\"http://www.google.com/\\">here</A>.\\r\\n</BODY></HTML>\\r\\n"\nread 219 bytes\nConn close\n"
分享给朋友:
您可能感兴趣的文章:
随机阅读: