-
Notifications
You must be signed in to change notification settings - Fork 14
/
python.html
208 lines (150 loc) · 18.2 KB
/
python.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<head>
<meta charset="utf-8">
<!-- Page title -->
<title>Unirest for Python - Simplified, lightweight HTTP Request Library</title>
<!-- Gotsta have that favicon-->
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<!-- Viewport -->
<meta name="viewport" content="width=480">
<!-- Stylesheet -->
<link href="styles/library.css" rel="stylesheet" type="text/css" data-inprogress="" />
<link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet" />
</head>
<body>
<nav>
<div class="right">
<a href="https://www.mashape.com/jobs">Created by Mashape <span>(We're Hiring!)</span></a>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="mashape" data-size="small" data-related="mashape" data-count="none">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<iframe src="http://ghbtns.com/github-btn.html?user=mashape&repo=unirest-python&type=fork&size=small" allowtransparency="true" frameborder="0" scrolling="0" width="52" height="20"></iframe>
</div>
<a href="http://unirest.io"><span class="icon-chevron-left"></span> Go Back</a>
</nav>
<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>
<div style="padding:30px 30px 25px" class="documentation">
<a id="user-content-unirest-for-python--" class="anchor" href="#unirest-for-python--" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Unirest for Python <a href="https://travis-ci.org/Mashape/unirest-python"><img src="https://camo.githubusercontent.com/f7b2bb716dea4e2e64ff9e44f8c76b73bb5b9a6a/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4d6173686170652f756e69726573742d707974686f6e2e7376673f7374796c653d666c6174" alt="Build Status" data-canonical-src="https://img.shields.io/travis/Mashape/unirest-python.svg?style=flat" style="max-width:100%;"></a> <a href="https://pypi.python.org/pypi/Unirest/"><img src="https://camo.githubusercontent.com/22825d25286bb6477b103a8232726836439f789c/68747470733a2f2f696d672e736869656c64732e696f2f707970692f762f556e69726573742e7376673f7374796c653d666c6174" alt="version" data-canonical-src="https://img.shields.io/pypi/v/Unirest.svg?style=flat" style="max-width:100%;"></a>
</h1>
<p><a href="https://github.com/Mashape/unirest-python/blob/master/LICENSE"><img src="https://camo.githubusercontent.com/9dcb37cea85910c15eefb8a75c0c45378a869c59/68747470733a2f2f696d672e736869656c64732e696f2f707970692f6c2f556e69726573742e7376673f7374796c653d666c6174" alt="License" data-canonical-src="https://img.shields.io/pypi/l/Unirest.svg?style=flat" style="max-width:100%;"></a>
<a href="https://pypi.python.org/pypi/Unirest/"><img src="https://camo.githubusercontent.com/61cd3e119fc284546c25440099472c72c2df99ee/68747470733a2f2f696d672e736869656c64732e696f2f707970692f646d2f556e69726573742e7376673f7374796c653d666c6174" alt="Downloads" data-canonical-src="https://img.shields.io/pypi/dm/Unirest.svg?style=flat" style="max-width:100%;"></a>
<a href="https://www.versioneye.com/user/projects/54b82a8905064657eb00024e"><img src="https://camo.githubusercontent.com/9e0a55857f629f06bfc32511e8b8e1831feedd64/68747470733a2f2f696d672e736869656c64732e696f2f76657273696f6e6579652f642f757365722f70726f6a656374732f3534623832613839303530363436353765623030303234652e7376673f7374796c653d666c6174" alt="Dependencies" data-canonical-src="https://img.shields.io/versioneye/d/user/projects/54b82a8905064657eb00024e.svg?style=flat" style="max-width:100%;"></a>
<a href="https://gitter.im/Mashape/unirest-python"><img src="https://camo.githubusercontent.com/5818ff993808659f636e910854b9aefcda215da2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4769747465722d4a6f696e253230436861742d626c75652e7376673f7374796c653d666c6174" alt="Gitter" data-canonical-src="https://img.shields.io/badge/Gitter-Join%20Chat-blue.svg?style=flat" style="max-width:100%;"></a></p>
<p><a href="https://camo.githubusercontent.com/899e266a5194998ba3da232ee9bc97c48deab283/687474703a2f2f636c2e6c792f696d6167652f32503337335930393073324f2f496d616765253230323031352d31302d31322532306174253230392e34382e3036253230504d2e706e67" target="_blank"><img src="https://camo.githubusercontent.com/899e266a5194998ba3da232ee9bc97c48deab283/687474703a2f2f636c2e6c792f696d6167652f32503337335930393073324f2f496d616765253230323031352d31302d31322532306174253230392e34382e3036253230504d2e706e67" alt="" data-canonical-src="http://cl.ly/image/2P373Y090s2O/Image%202015-10-12%20at%209.48.06%20PM.png" style="max-width:100%;"></a></p>
<p><a href="http://unirest.io">Unirest</a> is a set of lightweight HTTP libraries available in multiple languages, built and maintained by <a href="https://github.com/Mashape">Mashape</a>, who also maintain the open-source API Gateway <a href="https://github.com/Mashape/kong">Kong</a>. </p>
<h2>
<a id="user-content-features" class="anchor" href="#features" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Features</h2>
<ul>
<li>Make <code>GET</code>, <code>POST</code>, <code>PUT</code>, <code>PATCH</code>, <code>DELETE</code> requests</li>
<li>Both syncronous and asynchronous (non-blocking) requests</li>
<li>Supports form parameters, file uploads and custom body entities</li>
<li>Supports gzip</li>
<li>Supports Basic Authentication natively</li>
<li>Customizable timeout</li>
<li>Customizable default headers for every request (DRY)</li>
<li>Automatic JSON parsing into a native object for JSON responses</li>
</ul>
<h2>
<a id="user-content-installing" class="anchor" href="#installing" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Installing</h2>
<p>To utilize Unirest, install it using pip:</p>
<div class="highlight highlight-source-shell"><pre>$ pip install unirest</pre></div>
<p>After installing the pip package, you can now begin simplifying requests by importing unirest:</p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> unirest</pre></div>
<h3>
<a id="user-content-creating-requests" class="anchor" href="#creating-requests" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Creating Requests</h3>
<p>So you're probably wondering how using Unirest makes creating requests in Python easier, let's start with a working example:</p>
<div class="highlight highlight-source-python"><pre>response <span class="pl-k">=</span> unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>, <span class="pl-v">headers</span><span class="pl-k">=</span>{ <span class="pl-s"><span class="pl-pds">"</span>Accept<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span> }, <span class="pl-v">params</span><span class="pl-k">=</span>{ <span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>: <span class="pl-c1">23</span>, <span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>bar<span class="pl-pds">"</span></span> })
response.code <span class="pl-c"># The HTTP status code</span>
response.headers <span class="pl-c"># The HTTP headers</span>
response.body <span class="pl-c"># The parsed response</span>
response.raw_body <span class="pl-c"># The unparsed response</span></pre></div>
<h2>
<a id="user-content-asynchronous-requests" class="anchor" href="#asynchronous-requests" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Asynchronous Requests</h2>
<p>Python also supports asynchronous requests in which you can define a <code>callback</code> function to be passed along and invoked when Unirest receives the response:</p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">def</span> <span class="pl-en">callback_function</span>(<span class="pl-smi">response</span>):
response.code <span class="pl-c"># The HTTP status code</span>
response.headers <span class="pl-c"># The HTTP headers</span>
response.body <span class="pl-c"># The parsed response</span>
response.raw_body <span class="pl-c"># The unparsed response</span>
thread <span class="pl-k">=</span> unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>, <span class="pl-v">headers</span><span class="pl-k">=</span>{ <span class="pl-s"><span class="pl-pds">"</span>Accept<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span> }, <span class="pl-v">params</span><span class="pl-k">=</span>{ <span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>: <span class="pl-c1">23</span>, <span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>bar<span class="pl-pds">"</span></span> }, <span class="pl-v">callback</span><span class="pl-k">=</span>callback_function)</pre></div>
<h2>
<a id="user-content-file-uploads" class="anchor" href="#file-uploads" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>File Uploads</h2>
<p>Transferring file data requires that you <code>open</code> the file in a readable <code>r</code> mode:</p>
<div class="highlight highlight-source-python"><pre>response <span class="pl-k">=</span> unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>, <span class="pl-v">headers</span><span class="pl-k">=</span>{<span class="pl-s"><span class="pl-pds">"</span>Accept<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span>},
<span class="pl-v">params</span><span class="pl-k">=</span>{
<span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span>,
<span class="pl-s"><span class="pl-pds">"</span>file<span class="pl-pds">"</span></span>: <span class="pl-c1">open</span>(<span class="pl-s"><span class="pl-pds">"</span>/tmp/file<span class="pl-pds">"</span></span>, <span class="pl-v">mode</span><span class="pl-k">=</span><span class="pl-s"><span class="pl-pds">"</span>r<span class="pl-pds">"</span></span>)
}
)</pre></div>
<h2>
<a id="user-content-custom-entity-body" class="anchor" href="#custom-entity-body" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Custom Entity Body</h2>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> json
response <span class="pl-k">=</span> unirest.post(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/post<span class="pl-pds">"</span></span>, <span class="pl-v">headers</span><span class="pl-k">=</span>{ <span class="pl-s"><span class="pl-pds">"</span>Accept<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>application/json<span class="pl-pds">"</span></span> },
<span class="pl-v">params</span><span class="pl-k">=</span>json.dumps({
<span class="pl-s"><span class="pl-pds">"</span>parameter<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>value<span class="pl-pds">"</span></span>,
<span class="pl-s"><span class="pl-pds">"</span>foo<span class="pl-pds">"</span></span>: <span class="pl-s"><span class="pl-pds">"</span>bar<span class="pl-pds">"</span></span>
})
)</pre></div>
<p><strong>Note</strong>: For the sake of semplicity, even with custom entities in the body, the keyword argument is still <code>params</code> (instead of <code>data</code> for example). I'm looking for feedback on this.</p>
<h3>
<a id="user-content-basic-authentication" class="anchor" href="#basic-authentication" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Basic Authentication</h3>
<p>Authenticating the request with basic authentication can be done by providing an <code>auth</code> array like:</p>
<div class="highlight highlight-source-python"><pre>response <span class="pl-k">=</span> unirest.get(<span class="pl-s"><span class="pl-pds">"</span>http://httpbin.org/get<span class="pl-pds">"</span></span>, <span class="pl-v">auth</span><span class="pl-k">=</span>(<span class="pl-s"><span class="pl-pds">'</span>username<span class="pl-pds">'</span></span>, <span class="pl-s"><span class="pl-pds">'</span>password<span class="pl-pds">'</span></span>))</pre></div>
<h1>
<a id="user-content-request" class="anchor" href="#request" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Request</h1>
<div class="highlight highlight-source-python"><pre>unirest.get(url, <span class="pl-v">headers</span> <span class="pl-k">=</span> {}, <span class="pl-v">params</span> <span class="pl-k">=</span> {}, <span class="pl-v">auth</span> <span class="pl-k">=</span> (), <span class="pl-v">callback</span> <span class="pl-k">=</span> <span class="pl-c1">None</span>)
unirest.post(url, <span class="pl-v">headers</span> <span class="pl-k">=</span> {}, <span class="pl-v">params</span> <span class="pl-k">=</span> {}, <span class="pl-v">auth</span> <span class="pl-k">=</span> (), <span class="pl-v">callback</span> <span class="pl-k">=</span> <span class="pl-c1">None</span>)
unirest.put(url, <span class="pl-v">headers</span> <span class="pl-k">=</span> {}, <span class="pl-v">params</span> <span class="pl-k">=</span> {}, <span class="pl-v">auth</span> <span class="pl-k">=</span> (), <span class="pl-v">callback</span> <span class="pl-k">=</span> <span class="pl-c1">None</span>)
unirest.patch(url, <span class="pl-v">headers</span> <span class="pl-k">=</span> {}, <span class="pl-v">params</span> <span class="pl-k">=</span> {}, <span class="pl-v">auth</span> <span class="pl-k">=</span> (), <span class="pl-v">callback</span> <span class="pl-k">=</span> <span class="pl-c1">None</span>)
unirest.delete(url, <span class="pl-v">headers</span> <span class="pl-k">=</span> {}, <span class="pl-v">params</span> <span class="pl-k">=</span> {}, <span class="pl-v">auth</span> <span class="pl-k">=</span> (), <span class="pl-v">callback</span> <span class="pl-k">=</span> <span class="pl-c1">None</span>)</pre></div>
<ul>
<li>
<code>url</code> - Endpoint, address, or URI to be acted upon and requested information from in a string format.</li>
<li>
<code>headers</code> - Request Headers as an associative array</li>
<li>
<code>params</code> - Request Body as an associative array or object</li>
<li>
<code>auth</code> - The Basic Authentication credentials as an array</li>
<li>
<code>callback</code> - Asychronous callback method to be invoked upon result.</li>
</ul>
<h1>
<a id="user-content-response" class="anchor" href="#response" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Response</h1>
<p>Upon receiving a response, Unirest returns the result in the form of an Object. This object should always have the same keys for each language regarding to the response details.</p>
<ul>
<li>
<code>code</code> - HTTP Response Status Code (Example 200)</li>
<li>
<code>headers</code>- HTTP Response Headers</li>
<li>
<code>body</code>- Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays.</li>
<li>
<code>raw_body</code>- Un-parsed response body</li>
</ul>
<h1>
<a id="user-content-advanced-configuration" class="anchor" href="#advanced-configuration" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Advanced Configuration</h1>
<p>You can set some advanced configuration to tune Unirest-Python:</p>
<h3>
<a id="user-content-timeout" class="anchor" href="#timeout" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Timeout</h3>
<p>You can set a custom timeout value (in <strong>seconds</strong>):</p>
<div class="highlight highlight-source-python"><pre>unirest.timeout(<span class="pl-c1">5</span>) <span class="pl-c"># 5s timeout</span></pre></div>
<h3>
<a id="user-content-default-request-headers" class="anchor" href="#default-request-headers" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Default Request Headers</h3>
<p>You can set default headers that will be sent on every request:</p>
<div class="highlight highlight-source-python"><pre>unirest.default_header(<span class="pl-s"><span class="pl-pds">'</span>Header1<span class="pl-pds">'</span></span>,<span class="pl-s"><span class="pl-pds">'</span>Value1<span class="pl-pds">'</span></span>)
unirest.default_header(<span class="pl-s"><span class="pl-pds">'</span>Header2<span class="pl-pds">'</span></span>,<span class="pl-s"><span class="pl-pds">'</span>Value2<span class="pl-pds">'</span></span>)</pre></div>
<p>You can clear the default headers anytime with:</p>
<div class="highlight highlight-source-python"><pre>unirest.clear_default_headers()</pre></div>
<hr>
<p>Made with ♥ from the <a href="https://www.mashape.com/">Mashape</a> team</p>
</div>
<script src="script/script.js"></script>
<script type="text/javascript" src="//mashape.github.io/notification-bar/embed.js" async defer></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$('a img').parent().css('border-bottom', '0px')
</script>
<script type="text/javascript">
var analytics=analytics||[];analytics.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d2dq2ahtl5zl1z.cloudfront.NET/analytics.js/v1/"+e+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){analytics.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["identify","track","trackLink","trackForm","trackClick","trackSubmit","pageview","ab","alias","ready"];for(var s=0;s<i.length;s++)analytics[i[s]]=r(i[s])}; analytics.load("xsv6p66v1y");
</script>
</body>
</html>