我仍然在学习Swift,并且我正尝试通过我在Swift中编写的新iOS应用程序向我的Web服务发出POST请求。Swift:将标题添加到我的REST POST请求
我需要知道如何将2个头添加到我现有的代码中。也是我正确地添加参数?
我到目前为止有:
let myUrl = NSURL(string: "https://MY-MOBILE-SERVICE.azure-mobile.net/api/login");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
let postString = "[email protected]&password=123";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
print("error=\(error)")
return
}
print("response = \(response)")
}
task.resume()
这里有我需要添加到该请求的头:
X-谟 - 应用:45634243542434
接受:application/json
如何将这些标题附加到我的请求中?
我敢肯定,如果你打开的文档['NSMutableURLRequest'(https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/)你会找到r高效的方法。 – jcaron
你应该在@jcaron所说的文档中轻松找到它。另外,你可能想看看Alamofire。 – jrf