blob: 6e6e5c08184e2138beb7d7a91a684e5076840f71 [file] [log] [blame]
jcgregorio26c0cd72006-07-03 17:36:17 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="ref.css" type='text/css' />
5<link rel="first" href="ref.html" title='The httplib2 Library' />
6<link rel='contents' href='contents.html' title="Contents" />
7<link rel='last' href='about.html' title='About this document...' />
8<link rel='help' href='about.html' title='About this document...' />
9<link rel="prev" href="response-objects.html" />
10<link rel="parent" href="module-httplib2.html" />
11<link rel="next" href="about.html" />
12<meta name='aesop' content='information' />
13<title>1.1.4 Examples </title>
14</head>
15<body>
16<DIV CLASS="navigation">
17<div id='top-navigation-panel' xml:id='top-navigation-panel'>
18<table align="center" width="100%" cellpadding="0" cellspacing="2">
19<tr>
20<td class='online-navigation'><a rel="prev" title="1.1.3 Response Objects"
21 href="response-objects.html"><img src='previous.png'
22 border='0' height='32' alt='Previous Page' width='32' /></A></td>
23<td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive"
24 href="module-httplib2.html"><img src='up.png'
25 border='0' height='32' alt='Up One Level' width='32' /></A></td>
26<td class='online-navigation'><a rel="next" title="About this document ..."
27 href="about.html"><img src='next.png'
28 border='0' height='32' alt='Next Page' width='32' /></A></td>
29<td align="center" width="100%">The httplib2 Library</td>
30<td class='online-navigation'><a rel="contents" title="Table of Contents"
31 href="contents.html"><img src='contents.png'
32 border='0' height='32' alt='Contents' width='32' /></A></td>
33<td class='online-navigation'><img src='blank.png'
34 border='0' height='32' alt='' width='32' /></td>
35<td class='online-navigation'><img src='blank.png'
36 border='0' height='32' alt='' width='32' /></td>
37</tr></table>
38<div class='online-navigation'>
39<b class="navlabel">Previous:</b>
40<a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</A>
41<b class="navlabel">Up:</b>
42<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</A>
43<b class="navlabel">Next:</b>
44<a class="sectref" rel="next" href="about.html">About this document ...</A>
45</div>
46<hr /></div>
47</DIV>
48<!--End of Navigation Panel-->
49
50<H2><A NAME="SECTION002140000000000000000"></A><A NAME="httplib2-example"></A>
51<BR>
521.1.4 Examples
53</H2>
54
55<P>
56To do a simple <code>GET</code> request just supply the absolute URI
57of the resource:
58
59<P>
60<div class="verbatim"><pre>
61import httplib2
62h = httplib2.Http()
63resp, content = h.request("http://bitworking.org/")
64assert resp.status == 200
65assert resp['content-type'] == 'text/html'
66</pre></div>
67
68<P>
69Here is more complex example that does a PUT
70of some text to a resource that requires authentication.
71The Http instance also uses a file cache
72in the directory <code>.cache</code>.
73
74<P>
75<div class="verbatim"><pre>
76import httplib2
77h = httplib2.Http(".cache")
78h.add_credentials('name', 'password')
79resp, content = h.request("https://example.org/chap/2",
80 "PUT", body="This is text",
81 headers={'content-type':'text/plain'} )
82</pre></div>
83
84<P>
85Here is an example that connects to a server that
86supports the Atom Publishing Protocol.
87
88<P>
89<div class="verbatim"><pre>
90import httplib2
91h = httplib2.Http()
92h.add_credentials(myname, mypasswd)
93h.follow_all_redirects = True
94headers = {'Content-Type': 'application/atom+xml'}
95body = """&lt;?xml version="1.0" ?&gt;
96 &lt;entry xmlns="http://www.w3.org/2005/Atom"&gt;
97 &lt;title&gt;Atom-Powered Robots Run Amok&lt;/title&gt;
98 &lt;id&gt;urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a&lt;/id&gt;
99 &lt;updated&gt;2003-12-13T18:30:02Z&lt;/updated&gt;
100 &lt;author&gt;&lt;name&gt;John Doe&lt;/name&gt;&lt;/author&gt;
101 &lt;content&gt;Some text.&lt;/content&gt;
102&lt;/entry&gt;
103"""
104uri = "http://www.example.com/collection/"
105resp, content = h.request(uri, "POST", body=body, headers=headers)
106</pre></div>
107
108<P>
109
110<P>
111<IMG
112 WIDTH="556" HEIGHT="20" ALIGN="BOTTOM" BORDER="0"
113 SRC="img1.png"
114 ALT="\begin{center}\vbox{\input{modref.ind}
115}\end{center}">
116<P>
117
118<P>
119
120<DIV CLASS="navigation">
121<div class='online-navigation'>
122<p></p><hr />
123<table align="center" width="100%" cellpadding="0" cellspacing="2">
124<tr>
125<td class='online-navigation'><a rel="prev" title="1.1.3 Response Objects"
126 href="response-objects.html"><img src='previous.png'
127 border='0' height='32' alt='Previous Page' width='32' /></A></td>
128<td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive"
129 href="module-httplib2.html"><img src='up.png'
130 border='0' height='32' alt='Up One Level' width='32' /></A></td>
131<td class='online-navigation'><a rel="next" title="About this document ..."
132 href="about.html"><img src='next.png'
133 border='0' height='32' alt='Next Page' width='32' /></A></td>
134<td align="center" width="100%">The httplib2 Library</td>
135<td class='online-navigation'><a rel="contents" title="Table of Contents"
136 href="contents.html"><img src='contents.png'
137 border='0' height='32' alt='Contents' width='32' /></A></td>
138<td class='online-navigation'><img src='blank.png'
139 border='0' height='32' alt='' width='32' /></td>
140<td class='online-navigation'><img src='blank.png'
141 border='0' height='32' alt='' width='32' /></td>
142</tr></table>
143<div class='online-navigation'>
144<b class="navlabel">Previous:</b>
145<a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</A>
146<b class="navlabel">Up:</b>
147<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</A>
148<b class="navlabel">Next:</b>
149<a class="sectref" rel="next" href="about.html">About this document ...</A>
150</div>
151</div>
152<hr />
153<span class="release-info">Release 0.2, documentation updated on July 2, 2006.</span>
154</DIV>
155<!--End of Navigation Panel-->
156
157</BODY>
158</HTML>