来自表单的IOS Swift Post请求,带有身份验证和数据
我有下面的代码,在其中我试图向api发出post请求 当我通过bash运行下面的命令时,我得到一个创建的响应。然而,当我试图将其转换为swift并运行代码时,api拒绝了我的代码 有什么想法吗 http-a mike:password POST amount=1000 user=mike description=test1来自表单的IOS Swift Post请求,带有身份验证和数据,ios,swift,Ios,Swift,我有下面的代码,在其中我试图向api发出post请求 当我通过bash运行下面的命令时,我得到一个创建的响应。然而,当我试图将其转换为swift并运行代码时,api拒绝了我的代码 有什么想法吗 http-a mike:password POST amount=1000 user=mike description=test1 HTTP/1.1 201 Created Allow: GET, POST, HEAD, OPTIONS Connection: keep-alive Content-Typ
HTTP/1.1 201 Created
Allow: GET, POST, HEAD, OPTIONS
Connection: keep-alive
Content-Type: application/json
Date: Sun, 04 Sep 2016 10:45:39 GMT
Server: Apache/2.4.23 (Amazon) mod_wsgi/3.5 Python/2.7.10
Vary: Accept,Cookie
X-Frame-Options: SAMEORIGIN
transfer-encoding: chunked
{
"amount": "1000",
"created": "2016-09-04T10:45:39.432369Z",
"description": "test1",
"id": 18,
"owner": 2
}
我的viewcontroller.swift
import UIKit
class PostController: UIViewController {
@IBOutlet weak var firstname: UITextField!
@IBOutlet weak var lastname: UITextField!
@IBOutlet weak var instrument: UITextField!
@IBOutlet weak var test: UITextField!
@IBAction func buttonPost(sender: UIButton) {
print(postForm("amount=" + self.firstname.text! + " user=" + self.lastname.text! + " description=" + self.instrument.text!))
}
override func viewDidLoad() {
super.viewDidLoad()
firstname.autocorrectionType = .No
lastname.autocorrectionType = .No
instrument.autocorrectionType = .No
test.autocorrectionType = .No
//Looks for single or multiple taps.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(PostController.dismissKeyboard))
view.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func postForm(postString: String) -> String {
let url:NSURL = NSURL(string: "http://mybudget-env.hnfarjj5iy.ap-southeast-2.elasticbeanstalk.com/api/transactions/")!
let session = NSURLSession.sharedSession()
let username = "mike"
let password = "password"
let loginString = NSString(format: "%@:%@", username, password)
let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64LoginString = loginData.base64EncodedStringWithOptions([])
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let paramString = postString
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
let task = session.dataTaskWithRequest(request) {
(
let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
}
task.resume()
let greeting = postString
return greeting
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
您没有对请求设置内容类型,也没有在每个参数之间添加
&
您应该使用代理工具(如Charles)来检查在每种情况下是什么将您的设备留在了网络上,这样您就可以纠正所有不匹配的情况。谢谢您的建议。这听起来是个不错的主意,如果能看到POST请求,那就太好了。我将添加符号(&S),如何添加内容类型?Thanks in Advanced Content type是一个标头,说明http正文数据的类型,在您的示例中,它是URL编码的表单