Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tim Jensen
2,416 PointsApi Restful HTTP Post request
Hi All!,
I am trying to make a HTTP request to a API my friend created for me, however, I can't seem to get it to work.
I get this error: Optional({"error":"You need to sign in or sign up before continuing."})
I was kinda looking for a "wrong username or password error"
Here is the Code
var params = ["email":"example@gmail.com", "password":"12345678"] as Dictionary <String, String>
var testURL = NSURL(string: "https://blooming-plateau-7265.herokuapp.com/api/sessions")
var request = NSMutableURLRequest(URL: testURL!)
request.HTTPMethod = "POST"
var err: NSError?
request.addValue("text/html", forHTTPHeaderField: "Content-Type")
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err)
var session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if((error) != nil) {
println(error.localizedDescription)
}
var strData = NSString(data: data, encoding: NSASCIIStringEncoding)
println(strData)
})
task.resume()
3 Answers
Stephen Whitfield
16,771 PointsI just tested that URL using AFNetworking and I'm getting back the response you're looking for while passing in params.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"email" : @"gibbons@gmail.com",
@"password" : @"12344566"
};
NSString *urlString = @"https://blooming-plateau-7265.herokuapp.com/api/token";
[manager POST:urlString parameters:params constructingBodyWithBlock:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Resposne: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
Response:
Resposne: {
info = "Couldn't find a user with that email";
}
Stephen Whitfield
16,771 PointsSo it looks like you're getting a response back from the server, which is great, but it looks like maybe you need to have signed in before making requests and receiving the callbacks you want. I checked the same request on Postman and got the same response back.
EDIT: Actually I see what you want now. You're looking for a different callback. It looks like this is something your friend has to resolve on the backend. He needs to first check if the email that has been sent to the server exists in the database. Also If it does exist, but the password doesn't match, return an error message that reads "wrong username or password error" with a correlating status code.
Tim Jensen
2,416 PointsI am kinda looking for this responce..?
"{"info":"Logged in","user":{"id":1,"email":"admin@example.com","token":"76e740aecc5ed1e6163e10e91f84f03a","created_at":"2015-03-03T16:09:32.712Z","updated_at":"2015-03-03T18:12:14.166Z"}}"
Even if I type a correct account and password. I still get the error
Tim Jensen
2,416 PointsThe new address is https://blooming-plateau-7265.herokuapp.com/api/token
I still can't send the params with the body, however, I can send it with the URL but that is not correct.