मैं एक dataTaskWithUrl
है:आप डेटा टास्क WithUrl में शीर्षलेख कैसे जोड़ते हैं?
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()
मैं अपने काम के लिए हेडर जोड़ना चाहते हैं।
दूसरे शब्दों में, मैं तेजी से करने के लिए इस कोड को परिवर्तित करने के लिए करना चाहते हैं:
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;
}];
मैं dataRequests के लिए नया हूँ। मैं उद्देश्य सी कोड नहीं समझता लेकिन मैंने उस कोड को देखा जब मैंने अनुमान लगाया। मुझे हेडर का उपयोग करने की आवश्यकता है क्योंकि अगर मैं सीधे https://restcountries-v1.p.mashape.com/all पर जाने का प्रयास करता हूं, तो मुझे एक त्रुटि मिलती है। मुझे इस वेबसाइट से उद्देश्य सी कोड प्राप्त हुआ था: https://www.mashape.com/fayder/rest-countries-v1। सही दिशा में किसी भी मदद की बहुत सराहना की जाएगी।
धन्यवाद
बहुत बहुत धन्यवाद @ लियो! यह एक सम्मोहन की तरह काम करता है। –