/
python.html
17 lines (17 loc) · 1.14 KB
/
python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<section name="python" class="python">
<p class="ioDesc">In</p>
<pre class="incoming brush:python">
from urllib2 import Request, urlopen
<% if @contentType is "application/json": %>from json import dumps
<% else if @contentType is "application/x-www-form-urlencoded": %>from urllib import urlencode
<% end %>
<% if @contentType is "application/x-www-form-urlencoded": %>values = urlencode(<%= @body.join('') %>)
<% else if @contentType is "application/json": %>values = dumps(<%= @body.join('') %>)
<% else if @method isnt 'GET': %>values = <%= @helpers.escape @body.join('') %>
<% end %><% if @helpers.isNotEmpty @headers: %>headers = {<%= ("#{@helpers.escape header}: #{@helpers.escape value}" for header,value of @headers).join(", ") %>}
<% end %>request = Request("<%= @apiUrl %><%= @url %>"<% if @method in ['POST', 'PUT'] : %>, data=values<% end %><% if @helpers.isNotEmpty @headers: %>, headers=headers<% end %>)
<% if @method not in ['GET', 'POST'] : %>request.get_method = lambda: '<%= @method %>'
<% end %>response_body = urlopen(request).read()
print response_body
</pre>
</section>