¿De dónde salió setHTTPMethod, setURL y setHTTPBody en swift?

Estoy intentando implementar requestes HTTP.

Aquí está la implementación del objective C

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; 

Empecé por escrito:

  let request = NSMutableURLRequest() request .setValue(postLength, forHTTPHeaderField: "Content-Lenght") request .setValue(application/json, forHTTPHeaderField: Accept) 

1. La request json me está dando un error.

2. No puedo convertir setURL y SetHTTPBody del objective C a rápido. No pude encontrar la opción para estos.

Cualquier ayuda sería apreciada. Gracias.

Se han convertido en properties. La mayoría de los methods setter con un solo argumento se han convertido en properties.

El problema con su línea json es que no tenía comillas alnetworkingedor de "Aceptar".

 let request = NSMutableURLRequest() request.url = url request.HTTPMethod = "POST" request.setValue(postLength, forHTTPHeaderField:"Content-Length") request.setValue("application/json", forHTTPHeaderField:"Accept") request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type") request.postBody = postData 

Aquí es cómo lo hago rápidamente:

request var: NSMutableURLRequest = NSMutableURLRequest ()
request.URL = NSURL (cadena: url)

request.HTTPMethod = "POST"

request.setValue (postLength, forHTTPHeaderField: "Content-Length")

request.setValue ("application / json", paraHTTPHeaderField: "Content-Type")

request.HTTPBody = jsonData

  var request: NSMutableURLRequest? let HTTPMethod: String = "POST" var timeoutInterval: NSTimeInterval = 60 var HTTPShouldHandleCookies: Bool = false request = NSMutableURLRequest(URL: url) request!.HTTPMethod = HTTPMethod request!.timeoutInterval = timeoutInterval request!.HTTPShouldHandleCookies = HTTPShouldHandleCookies let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy" let contentType = "multipart/form-data; boundary=\(boundary)" request!.setValue(contentType, forHTTPHeaderField:"Content-Type") var body = NSMutableData(); let tempData = NSMutableData() let fileName = filenames + ".jpg" //"testing.jpg" let parameterName = "userfile" let mimeType = "application/octet-stream" tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) let fileNameContentDisposition = fileName != nil ? "filename=\"\(fileName)\"" : "" let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\"; \(fileNameContentDisposition)\r\n" tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding)!) tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) tempData.appendData(imageData) tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) body.appendData(tempData) body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) request!.setValue("\(body.length)", forHTTPHeaderField: "Content-Length") request!.HTTPBody = body var vl_error :NSErrorPointer = nil var responseData = NSURLConnection.sendSynchronousRequest(request,returningResponse: nil, error:vl_error) var results = NSString(data:responseData, encoding:NSUTF8StringEncoding) println("finish \(results)") 
 I am calling the json on login button click @IBAction func loginClicked(sender : AnyObject){ var request = NSMutableURLRequest(URL: NSURL(string: kLoginURL)) // Here, kLogin contains the Login API. var session = NSURLSession.shanetworkingSession() request.HTTPMethod = "POST" var err: NSError? request.HTTPBody = NSJSONSerialization.dataWithJSONObject(self.criteriaDic(), options: nil, error: &err) // This Line fills the web service with requinetworking parameters. request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in var strData = NSString(data: data, encoding: NSUTF8StringEncoding) println("Body: \(strData)") var err1: NSError? var json2 = NSJSONSerialization.JSONObjectWithData(strData.dataUsingEncoding(NSUTF8StringEncoding), options: .MutableLeaves, error:&err1 ) as NSDictionary if(err) { println(err!.localizedDescription) } else { var success = json2["success"] as? Int println("Succes: \(success)") } }) task.resume() } Here, I have made a seperate dictionary for the parameters. var params = ["format":"json", "MobileType":"IOS","MIN":"f8d16d98ad12acdbbe1de647414495ec","UserName":emailTxtField.text,"PWD":passwordTxtField.text,"SigninVia":"SH"] as NSDictionary return params }