Ich habe diese API http://my-api.mydoctorfinder.com/ , die einen Bool-Wert in Abhängigkeit von der E-Mail und Passwort zurückkehren Sie eingegeben haben.Wie POST-Anfrage in Swift
Mein Problem ist, es wird immer falsch zurückgegeben, obwohl die richtige E-Mail-Adresse und das richtige Passwort verwendet werden.
Ich dachte, dass ich nicht den richtigen Parameter gesendet haben könnte, seit ich ein Wörterbuch erstellt habe, das die E-Mail und das Passwort enthält. Dann ging es auf NSJSONSerialization.dataWithJSONObject Methode
By the way, ich SwiftyJson wurde mit.
Dies ist mein Code
//creates a dictionary and calls the PostRequest method
func attemptLogIn(email: String, password: String) {
let route = loggerURL
let body: [String:String] = ["email":email, "password":password]
makeHTTPPostRequest(route, body: body)
}
//performs post request
private func makeHTTPPostRequest(path: String, body: [String: AnyObject]) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
// Set the method to POST
request.HTTPMethod = "POST"
do {
// Set the POST body for the request
let jsonBody = try NSJSONSerialization.dataWithJSONObject(body, options: .PrettyPrinted)
request.HTTPBody = jsonBody
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if let jsonData = data {
let json:JSON = JSON(data: jsonData)
//onCompletion(json, nil)
print("The Response: ")
print(json)
} else {
//onCompletion(nil, error)
print("The Response: ")
print("Hello")
}
})
task.resume()
} catch {
// Create your personal error
//onCompletion(nil, nil)
}
}
Ihre API-Dokumentation macht es nicht sofort offensichtlich, ob sie eine 'application/json'-Anfrage oder eine 'application/x-www-form-urlencoded'-Anfrage oder XML oder was auch immer erwartet. (Und nur weil Sie möglicherweise JSON-Antwort generieren, bedeutet dies nicht unbedingt, dass die Anfrage auch JSON ist.) Ihr Code schlägt vor, dass die Anfrage JSON sein sollte, aber wenn ja, sollten Sie Ihren Header "Content-Type" entsprechend einstellen von Josip beschrieben. Übrigens sagst du, dass es "immer [s] falsch zurückgibt". Wie sieht die "Antwort" aus? Der "Fehler"? Wenn das Parsen der JSON fehlgeschlagen ist, was enthalten die 'Daten'? – Rob