Skip to content

Instantly share code, notes, and snippets.

@saiten
Created June 23, 2015 07:52
Show Gist options
  • Save saiten/6d847c1f97d5d1580082 to your computer and use it in GitHub Desktop.
Save saiten/6d847c1f97d5d1580082 to your computer and use it in GitHub Desktop.
import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely()
func postAsync() {
// create the url-request
let urlString = "http://api.search.nicovideo.jp/api/snapshot/"
//let urlString = "http://www.google.com"
let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
// set the method(HTTP-POST)
request.HTTPMethod = "POST"
// set the header(s)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
// set the request-body(JSON)
let params: [String: AnyObject] = [
"query":"初音ミク",
"service":["video"],
"search":["title","description","tags"],
"join":["cmsid","start_time", "title","view_counter"],
"filters":[["type": "range", "field": "start_time", "from": "2015-06-19 00:00:00", "to": "2015-06-19 23:59:59", "include_lower": true, "include_upper": true]],
"from":0,
"sort_by":"view_counter",
"issuer":"yourservice/applicationname"
]
let opt: NSJSONWritingOptions = NSJSONWritingOptions.PrettyPrinted
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: opt)
} catch {
print(error)
}
// use NSURLSessionDataTask
let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: {data, response, error in
if (error == nil) {
let result = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(result)
} else {
print(error)
}
})
task!.resume()
}
postAsync()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment