J'essaie de faire quelque chose comme ça:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]];
request.HTTPMethod = @"POST";
NSString *stringData = @"some data";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
C'est ce que j'ai jusqu'ici:
var url = NSURL(string: "some url")
var request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
var dataString = "some data"
var requestBodyData: NSData = dataString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) as NSData
request.HTTPBody = requestBodyData
var connection = NSURLConnection(request: request, delegate: self, startImmediately: false)
println("sending request...")
connection.start()
Cependant, la ligne var requestBodyData: lance la première pierre avec "Impossible de convertir le type NSData de l'expression en type NSData".
Quelqu'un veut-il peser?
stringByAddingPercentEscapesUsingEncoding
renvoie une chaîne au lieu d'un objet NSData. Selon this answer, vous devez l’utiliser pour convertir une instance NSData:
let data = (anySwiftString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
Utilisation de Alamofire pour envoyer une demande HTTP POST
let request = NSMutableURLRequest(URL: NSURL(string: "https://www.google.com")!)
request.HTTPMethod = "POST"
let body = "email=\(email)&access_token=\(token)"
request.HTTPBody = body.dataUsingEncoding(NSUTF8StringEncoding)
Alamofire.request(request).responseJSON { response in
NSLog("response.request: \(response.request)") // original URL request
NSLog("esponse.response: \(response.response)") // URL response
switch response.result {
case .Success:
NSLog("Validation Successful")
case .Failure(let error):
// Logout
NSLog("\(error), \(String(data: response.data!, encoding: NSUTF8StringEncoding))")
return
}
// Save sip account information to NSUserdefaults
if let json = response.result.value as? [String: AnyObject]{
// Do your stuff
}
}