[plt-scheme] Re: How to get HTTP headers using bzlib/http?

From: YC (yinso.chen at gmail.com)
Date: Sun Dec 13 12:54:55 EST 2009

On Sun, Dec 13, 2009 at 5:20 AM, ccwu <ccwu660601 at gmail.com> wrote:

> Ok, now I know the answer.
> I've thought that (http->get url) returns an input-port, but it
> returns an http-client-response instead.
>
>
Correct.  http-get returns http-client-response, which wraps over both the
headers and a pure input-port (without the headers).  You can use
http-client-response-headers to retrieve the headers, which are key/value
pairs, and you can retrieve the response as an input port to retrieve and
manipulate the body.  Use close-input-port when you are done so the TCP
connection can be released.

Below is an example repl session.  Cheers,
yc

Welcome to DrScheme, version 4.2.1 [3m].
Language: Module; memory limit: 128 megabytes.
> (require (planet bzlib/http))
> (define response (http-get "http://www.google.com/"))
|(reader #<input-port:www.google.com>)
|"HTTP/1.0 200 OK"
> response
#<http-client-response>
> (http-client-response-headers response)
(("X-XSS-Protection" . "0")
 ("Server" . "gws")
 ("Set-Cookie"
  .

"NID=29=UrP_GBX8LIjMuHubAxlXD5Uzo2m1tz5GqvJaZqq8tLPbo5kkcg56DT5BgeKLtN3WyYqbcMSJwzZWQC5T8ONvj68FSJRf2ehC8SXyl63pDlgLuP5lMpElmcCRKbsCDn_i;
expires=Mon, 14-Jun-2010 17:47:24 GMT; path=/; domain=.google.com;
HttpOnly")
 ("Set-Cookie"
  .
  "PREF=ID=1453ea3d4dd38bd4:TM=1260726444:LM=1260726444:S=Gdsq2rwddhYWBX1N;
expires=Tue, 13-Dec-2011 17:47:24 GMT; path=/; domain=.google.com")
 ("Content-Type" . "text/html; charset=ISO-8859-1")
 ("Cache-Control" . "private, max-age=0")
 ("Expires" . "-1")
 ("Date" . "Sun, 13 Dec 2009 17:47:24 GMT"))
> (read-bytes 2048 response)
#"<!doctype html><html><head><meta http-equiv=\"content-type\"
content=\"text/html;
charset=ISO-8859-1\"><title>Google</title><script>window.google={kEI:\"rCglS_TNGZrkpgSZt-XeCQ\",kEXPI:\"17259,22452,23061,23129,23179\",kCSI:{e:\"17259,22452,23061,23129,23179\",ei:\"rCglS_TNGZrkpgSZt-XeCQ\"},kHL:\"en\",time:function(){return(new
Date).getTime()},log:function(b,d,c){var a=new
Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete
g[f]}));g[f]=a;c=c||\"/gen_204?atyp=i&ct=\"+b+\"&cad=\"+d+\"&zx=\"+google.time();a.src=c;
e.li=f+1},lc:[],li:0};\nwindow.google.sn=\"webhp\";window.google.timers={load:{t:{start:(new
Date).getTime()}}};try{}catch(u){}window.google.jsrt_kill=1;\nvar
_gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf(\"#\");if(e>=0){var
a=_gjwl.href.substring(e);if(a.indexOf(\"&q=\")>0||a.indexOf(\"#q=\")>=0){a=a.substring(1);if(a.indexOf(\"#\")==-1){for(var
c=0;c<a.length;){var d=c;if(a.charAt(d)==\"&\")++d;var
b=a.indexOf(\"&\",d);if(b==-1)b=a.length;var
f=a.substring(d,b);if(f.indexOf(\"fp=\")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else
if(f==\"cad=h\")return 0;c=b}_gjwl.href=\"/search?\"+a+\"&cad=h\";return
1}}}return 0}function
_gjp(){!(window._gjwl.hash&&\nwindow._gjuc())&&setTimeout(_gjp,500)};\nwindow._gjp
&& _gjp()</script><style>td{line-height:.8em;}.gac_m
td{line-height:17px;}form{margin-bottom:20px;}body,td,a,p,.h{font-family:arial,sans-serif}.h{color:#36c;font-size:20px}.q{color:#00c}.ts
td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{font:17px
arial,sans-serif;margin-bottom:.2em;vertical-align:bottom;}input{font-family:inherit}.lsb,.gac_sb{font-size:15px;height:1.85em!important;margin:.2em;}#gbar{float:left;height:22px}.gbh,.gbd{border-top:1px
solid
#c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}#gbs,.gbm{background:#fff;left:0;position:absolute;text-align:left;visibility:hidden;z-index:1000}.gbm{border:1px
solid;border-color:#c9d7f1 #36c #36c
#a2bae7;z-index:1001}#guser{padding-bottom:7px
!important;text-align:right}#gbar,#guser{font-siz"
> (close-input-port response)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20091213/e180494e/attachment.html>

Posted on the users mailing list.