I am trying to do a simple POST request with Form-Data to an api but I always get a status 401 in return.
I can easily make the call outside of xcode (in Postman for instance) with success, all I do is POST to the url with key-value pairs of type Form-Data but in xcode it always fails.
Here is my AFHTTPClient:
@implementation UnpaktAPIClient
+ (id)sharedInstance {
static UnpaktAPIClient *__sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__sharedInstance = [[UnpaktAPIClient alloc] initWithBaseURL:[NSURL URLWithString:UnpaktAPIBaseURLString]];
});
return __sharedInstance;
}
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setParameterEncoding:AFFormURLParameterEncoding];
}
return self;
}
- (NSDictionary *)login:(NSDictionary *)credentials {
[[UnpaktAPIClient sharedInstance] postPath:@"/api/v1/users/login"
parameters:credentials
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"success");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error){
NSLog(@"%@", error);
}];
return nil;
}
I've also attempted creating a request with a requestOperation:
- (NSDictionary *)login:(NSDictionary *)credentials {
NSMutableURLRequest *request = [[UnpaktAPIClient sharedInstance] requestWithMethod:@"POST" path:@"/api/v1/users/login" parameters:credentials];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"success");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json){
NSLog(@"%@", error);
}];
[operation start];
return nil;
}
But I get the exact same error all the time: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 401"
. I'm expecting JSON feedback, you can see the success condition in the attached picture. I suspect it's something very simple but I'm pulling my hair out trying to find it, I'm quite new to networking. Any help would be awesome, thanks.
UPDATE
Since the Postman request has blank params and headers, I figured I needed to get to the form body. Changing the request to:
- (NSDictionary *)login:(NSDictionary *)credentials {
NSURLRequest *request = [self multipartFormRequestWithMethod:@"POST"
path:@"/api/v1/users/login"
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
}];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json) {
NSLog(@"success");
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id json) {
NSLog(@"%@", error);
}];
[operation start];
return nil;
}
Now gives me a 404 error, which is what I would expect without a valid email and password, I just need to figure out how to add one to the body of the form, anything I try to add gives me the "request body stream exhausted" error.
Got it, turns out it's a little verbose to add key/value pairs to form-data but I accomplished it like so:
NSMutableData *email = [[NSMutableData alloc] init];
NSMutableData *password = [[NSMutableData alloc] init];
[email appendData:[[NSString stringWithFormat:@"[email protected]"] dataUsingEncoding:NSUTF8StringEncoding]];
[password appendData:[[NSString stringWithFormat:@"qwerty"] dataUsingEncoding:NSUTF8StringEncoding]];
[formData appendPartWithFormData:email name:@"email"];
[formData appendPartWithFormData:password name:@"password"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With