The Well-Formed Web

Exploring the limits of XML and HTTP

Content Negotiation Example

Here is a client-side example Python script that exploits the content negotiation in RESTLog.


import urllib
# Simple test on WellFormedWeb.org to test the content
# negotiation. This file contains two tests. In each
# test a GET request is made against the URL
# , the only
# difference being in the ACCEPT header that is sent.
# In the first test the RSS file is returned,
# while in the second test the HTML file is
# returned.

op = urllib.FancyURLopener()
op.addheader('Accept', 'application/rss+xml')
f = op.open('/RESTLog.cgi')
s = f.read()
if (-1 != s.find("<description>")):
    print "Success, we received an RSS file."

op = urllib.FancyURLopener()
op.addheader('Accept', 'text/html')
f = op.open('/RESTLog.cgi')
s = f.read()
if (-1 != s.find("<head>")):
    print "Success, we received an HTML file."

2002-11-14 23:18 Comments (0)