blob: 1b245a1a320cb9a610897f5a4a1f04bd313f5587 [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>
jcgregoriocde4f092007-03-08 21:59:04 +0000109Here is an example of providing data to an HTML form processor.
110In this case we presume this is a POST form. We need to take our
111data and format it as "application/x-www-form-urlencoded" data and use that as a
112body for a POST request.
113
114<P>
115<div class="verbatim"><pre>
116&gt;&gt;&gt; import httplib2
117&gt;&gt;&gt; import urllib
118&gt;&gt;&gt; data = {'name': 'fred', 'address': '123 shady lane'}
119&gt;&gt;&gt; body = urllib.urlencode(data)
120&gt;&gt;&gt; body
121'name=fred&amp;address=123+shady+lane'
122&gt;&gt;&gt; h = httplib2.Http()
123&gt;&gt;&gt; resp, content = h.request("http://example.com", method="POST", body=body)
124</pre></div>
jcgregorio26c0cd72006-07-03 17:36:17 +0000125
126<P>
127<IMG
128 WIDTH="556" HEIGHT="20" ALIGN="BOTTOM" BORDER="0"
129 SRC="img1.png"
130 ALT="\begin{center}\vbox{\input{modref.ind}
131}\end{center}">
132<P>
133
134<P>
135
136<DIV CLASS="navigation">
137<div class='online-navigation'>
138<p></p><hr />
139<table align="center" width="100%" cellpadding="0" cellspacing="2">
140<tr>
141<td class='online-navigation'><a rel="prev" title="1.1.3 Response Objects"
142 href="response-objects.html"><img src='previous.png'
143 border='0' height='32' alt='Previous Page' width='32' /></A></td>
144<td class='online-navigation'><a rel="parent" title="1.1 httplib2 A comprehensive"
145 href="module-httplib2.html"><img src='up.png'
146 border='0' height='32' alt='Up One Level' width='32' /></A></td>
147<td class='online-navigation'><a rel="next" title="About this document ..."
148 href="about.html"><img src='next.png'
149 border='0' height='32' alt='Next Page' width='32' /></A></td>
150<td align="center" width="100%">The httplib2 Library</td>
151<td class='online-navigation'><a rel="contents" title="Table of Contents"
152 href="contents.html"><img src='contents.png'
153 border='0' height='32' alt='Contents' width='32' /></A></td>
154<td class='online-navigation'><img src='blank.png'
155 border='0' height='32' alt='' width='32' /></td>
156<td class='online-navigation'><img src='blank.png'
157 border='0' height='32' alt='' width='32' /></td>
158</tr></table>
159<div class='online-navigation'>
160<b class="navlabel">Previous:</b>
161<a class="sectref" rel="prev" href="response-objects.html">1.1.3 Response Objects</A>
162<b class="navlabel">Up:</b>
163<a class="sectref" rel="parent" href="module-httplib2.html">1.1 httplib2 A comprehensive</A>
164<b class="navlabel">Next:</b>
165<a class="sectref" rel="next" href="about.html">About this document ...</A>
166</div>
167</div>
168<hr />
jcgregoriocde4f092007-03-08 21:59:04 +0000169<span class="release-info">Release 0.3, documentation updated on Mar 8, 2007.</span>
jcgregorio26c0cd72006-07-03 17:36:17 +0000170</DIV>
171<!--End of Navigation Panel-->
172
173</BODY>
174</HTML>