0
我有body token和customer_id。我想進入身體。我也想傳遞包含x-access-token的頭文件。這裏是我的代碼現在如何使用NSMutableURLRequest或Alamofire
func sendToken(token:PSTCKToken, customer_id: Int){
let url = NSURL(string: "urlGoesHERE")!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("x-access-token", forHTTPHeaderField: "Bearer xxxxxxxxxxxx")
let postBody = "token=\(token)&customer_id=\(customer_id)"
let postData = postBody.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let session = NSURLSession.sharedSession()
session.uploadTaskWithRequest(request, fromData: postData, completionHandler: { data, response, error in
let successfulResponse = (response as? NSHTTPURLResponse)?.statusCode == 200
if successfulResponse && error == nil && data != nil{
// All was well
let newStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(newStr) // All we did here is log it to the output window
} else {
if let e=error {
print(e.description)
} else {
// There was no error returned though status code was not 200
print("There was an error communicating with payment backend.")
// All we did here is log it to the output window
}
}
}).resume()
}
這裏使用NSMutableURLRequest 我的代碼,我怎麼能做到這一點與NSMutableURLRequest或Alamofire
謝謝你的工作! – lordxxx