Reputation: 541
I have the below code in which I'm trying to make a post request to an api.
When I run the below through bash I get a created response. However when I'm trying to convert it to swift and run the code the api rejects my code.
Any thoughts?
http -a mike:password POST http://mybudget-env.hnfarjj5iy.ap-southeast-2.elasticbeanstalk.com/api/transactions/ amount=1000 user=mike description=test1
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
}
My 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.
}
*/
}
Upvotes: 1
Views: 246
Reputation: 119031
You aren't setting a content type on your request and you aren't adding the &
between each of the parameters.
You should use a proxy tool, like Charles, to check what actually leaves your device onto the network in each case so you can correct all the mismatches.
Upvotes: 2