This module provides a convenient alternative to NSMutableURLRequest, replicating d3-request. It is experimental and not recommended to be used in production environments.
To load a HTML file:
request("https://www.google.com")?
.mimeType("text/html")
.send("GET") {
(error: NSError?, response: NSHTTPURLResponse?, data: NSData?) in
print(response)
}
To load and parse a JSON file:
json("https://api.github.com/users/apple/repos") {
(error: NSError?, json: AnyObject?) in
print(json)
}
To post some query parameters:
request("/path/to/resource")?
.header("Content-Type", "application/x-www-form-urlencoded")
.post("a=2&b=3", completion: { (error, response, data) -> Void in
print(response)
})
It currently has built-in support for parsing JSON only.
#request(url[, callback])
Returns a new asynchronous request for specified url. If no callback is specified, the request is not yet sent and can be further configured. If a callback is specified, it is equivalent to calling request.get immediately after construction:
request(url)
.get(callback);
Note: if you wish to specify a request header or a mime type, you must not specify a callback to the constructor. Use request.header or request.mimeType followed by request.get instead.
# request.header(name[, value])
If value is specified, sets the request header with the specified name to the specified value and returns this request instance. If value is null, removes the request header with the specified name instead. If value is not specified, returns the current value of the request header with the specified name. Header names are case-insensitive.
Request headers can only be modified before the request is sent. Therefore, you cannot pass a callback to the request constructor if you wish to specify a header; use request.get or similar instead. For example:
request(url)
.header("Accept-Language", "en-US")
.get(callback);
# request.mimeType([type])
If type is specified, sets the request mime type to the specified value and returns this request instance. If type is null, clears the current mime type (if any) instead. If type is not specified, returns the current mime type, which defaults to null. The mime type is used to both set the "Accept" request header and for overrideMimeType, where supported.
The request mime type can only be modified before the request is sent. Therefore, you cannot pass a callback to the request constructor if you wish to override the mime type; use request.get or similar instead. For example:
request(url)
.mimeType("text/csv")
.get(callback);
# request.get([callback])
Equivalent to request.send with the GET method:
request.send("GET", callback);
# request.post([data][, callback])
Equivalent to request.send with the POST method:
request.send("POST", data, callback);
# request.send(method[, data][, callback])
Issues this request using the specified method (such as "GET"
or "POST"
), optionally posting the specified data in the request body, and returns this request instance. If a callback is specified, the callback will be invoked asynchronously when the request succeeds or fails. The callback is invoked with two arguments: the error, if any, and the response value.
# d3.json(url[, callback])
Creates a request for the JSON file at the specified url with the default mime type "application/json"
.