Ho un dataTaskWithUrl
:Come si aggiungono le intestazioni a dataTaskWithUrl?
var headers: NSDictionary = ["X-Mashape-Key": "my-secret-key" , "Accept" : "application/json"]
var stringUrl = "https://restcountries-v1.p.mashape.com/all"
stringUrl = stringUrl.stringByReplacingOccurrencesOfString(" ", withString: "+")
let url = NSURL(string: stringUrl)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary{
println(jsonResult)
}else{
println("error")
}
})
task.resume()
voglio aggiungere intestazioni al mio compito.
In altre parole, vorrei convertire questo codice swift:
NSDictionary *headers = @{@"X-Mashape-Key": @"my-secret-key", @"Accept": @"application/json"};
UNIUrlConnection *asyncConnection = [[UNIRest get:^(UNISimpleRequest *request) {
[request setUrl:@"https://restcountries-v1.p.mashape.com/all"];
[request setHeaders:headers];
}] asJsonAsync:^(UNIHTTPJsonResponse *response, NSError *error) {
NSInteger code = response.code;
NSDictionary *responseHeaders = response.headers;
UNIJsonNode *body = response.body;
NSData *rawBody = response.rawBody;
}];
Sono nuovo di dataRequests. Non capisco il codice Objective C ma ho fatto un'ipotesi quando ho guardato quel codice. Ho bisogno di usare le intestazioni perché se provassi semplicemente ad andare a https://restcountries-v1.p.mashape.com/all direttamente, ricevo un errore. Ho ricevuto questo codice Objective C da questo sito Web: https://www.mashape.com/fayder/rest-countries-v1. Qualsiasi aiuto nella giusta direzione sarebbe molto apprezzato.
Grazie
Grazie mille @Leo! Esso funziona magicamente. –