Quick search

Table Of Contents

Url Request

New in version 1.0.8.

You can use the UrlRequest to make asynchronous request on the web, and get the result when the request is completed. The spirit is the same as XHR object in Javascript.

The content is also decoded, aka for now, if the Content-Type is application/json, the result will be automatically passed through json.loads.

The syntax to create a request:

from kivy.network.urlrequest import UrlRequest
req = UrlRequest(url, callback_success, callback_error, body, headers)

Only the first argument is mandatory, all the rest is optional. By default, a “GET” request will be done. If UrlRequest.req_body is not None, a “POST” request will be done. It’s up to you to adjust UrlRequest.req_headers if necessary.

Example of fetching twitter trends:

def got_twitter_trends(req, result):
    trends = result[0]['trends']
    print 'Last %d twitter trends:' % len(trends),
    for trend in trends:
        print trend['name'],
    print '!'

req = UrlRequest('https://api.twitter.com/1/trends/1.json',
        got_twitter_trends)

Example of Posting data (adapted from httplib example):

import urllib

def bug_posted(req, result):
    print 'Our bug is posted !'
    print result

params = urllib.urlencode({'@number': 12524, '@type': 'issue',
    '@action': 'show'})
headers = {'Content-type': 'application/x-www-form-urlencoded',
          'Accept': 'text/plain'}
req = UrlRequest('bugs.python.org', on_success=bug_posted, req_body=params,
        req_headers=headers)
class kivy.network.urlrequest.UrlRequest(url, on_success=None, on_error=None, on_progress=None, req_body=None, req_headers=None, chunk_size=8192, timeout=None, method=None, debug=False)

Bases: threading.Thread

Url request. See module documentation for usage.

Changed in version 1.5.1: Add debug parameter

Changed in version 1.0.10: Add method parameter

Parameters :
url: str

Complete url string to call.

on_success: callback(request, result)

Callback function to call when the result have been fetched

on_error: callback(request, error)

Callback function to call when an error happen

on_progress: callback(request, current_size, total_size)

Callback function that will be called to report progression of the download. total_size might be -1 if no Content-Length have been reported in the http response. This callback will be called after each chunk_size read.

req_body: str, default to None

Data to sent in the request. If it’s not None, a POST will be done instead of a GET

req_headers: dict, default to None

Custom headers to add for the request

chunk_size: int, default to 8192

Size of each chunk to read, used only when on_progress callback have been set. If you decrease it too much, a lot of on_progress will be fired, and will slow down your download. If you want to have the maximum download speed, increase chunk_size, or don’t use on_progress.

timeout: int, default to None

If set, blocking operations will timeout after that many seconds.

method: str, default to ‘GET’ (or ‘POST’ if body)

HTTP method to use

debug: bool, default to False

If True, it will use the Logger.debug to print information about url access/progression/error.

chunk_size

Return the size of a chunk, used only in “progress” mode (when on_progress callback is set.)

decode_result(result, resp)

Decode the result fetched from url according to his Content-Type. Actually, only decode application/json.

error

Return the error of the request. This value is not undeterminate until the request is finished.

get_connection_for_scheme(scheme)

Return the Connection class from a particular scheme. This is an internal that can be expanded to support custom scheme.

Actual supported schemes: http, https.

is_finished

Return True if the request have finished, whatever is if it’s a success or a failure.

req_body = None

Request body passed in __init__

req_headers = None

Request headers passed in __init__

resp_headers

If the request have been done, return a dictionnary containing the headers of the response. Otherwise, it will return None

resp_status

Return the status code of the response if the request have been done, otherwise, return None

result

Return the result of the request. This value is not undeterminate until the request is finished.

url = None

Url of the request

wait(delay=0.5)

If you want a sync request, you can call the wait() method. It will wait for the request to be finished (until resp_status is not None)

Note

This method is intended to be used in the main thread, and the callback will be dispatched from the same thread as the thread you’re calling it.

New in version 1.1.0.