I'm trying to upload an image to an image recognition API, but I'm getting this is the NSHTTPURLResponse
I'm getting.
<NSHTTPURLResponse: 0x12f7ce4e0> { URL: https:/api.vize.ai/v1/classify/ } { status code: 400, headers {
Allow = "POST, OPTIONS";
Connection = "keep-alive";
"Content-Language" = en;
"Content-Length" = 37;
"Content-Type" = "application/json";
Date = "Wed, 21 Feb 2018 12:24:17 GMT";
"Referrer-Policy" = "strict-origin";
Server = "nginx/1.13.7";
"Strict-Transport-Security" = "max-age=31536000; includeSubDomains; preload";
Vary = "Accept, Host, Accept-Language, Cookie";
"X-Frame-Options" = DENY;
"X-UA-Compatible" = "IE=Edge,chrome=1";
"x-content-type-options" = nosniff;
"x-xss-protection" = "1; mode=block";
} }
I'm using an image picker to select an image, and then upload that. Here's my implementation of that.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// Dismiss image picker
[picker dismissViewControllerAnimated:YES completion:nil];
// Classify image
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
// Request authorization
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized:
NSLog(@"PHAuthorizationStatusAuthorized");
break;
case PHAuthorizationStatusDenied:
NSLog(@"PHAuthorizationStatusDenied");
break;
case PHAuthorizationStatusNotDetermined:
NSLog(@"PHAuthorizationStatusNotDetermined");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"PHAuthorizationStatusRestricted");
break;
}
}];
// Get image name
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];
// Get image file path and append file name onto it
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[imageData writeToFile:path atomically:YES];
NSDictionary *headers = @{ @"content-type": @"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
@"Content-Type": @"multipart/form-data",
@"Accept": @"application/json",
@"Authorization": @"Token 60f6be2a21bdf731d86a8817b440a1afba692fed",
@"Cache-Control": @"no-cache",
@"Postman-Token": @"7d262730-0c7d-66dc-bbbb-43f40dbfe8ce" };
NSArray *parameters = @[ @{ @"name": @"task", @"value": @"dc9ef71a-b8a0-4a12-90fd-83d717cf887f" },
@{ @"name": @"image_file", @"fileName": path } ];
NSString *boundary = @"----WebKitFormBoundary7MA4YWxkTrZu0gW";
NSError *error;
NSMutableString *body = [NSMutableString string];
for (NSDictionary *param in parameters) {
[body appendFormat:@"--%@\r\n", boundary];
if (param[@"fileName"]) {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\"\r\n", param[@"name"], param[@"fileName"]];
[body appendFormat:@"Content-Type: %@\r\n\r\n", param[@"contentType"]];
[body appendFormat:@"%@", [NSString stringWithContentsOfFile:param[@"fileName"] encoding:/*NSASCIIStringEncoding*/NSASCIIStringEncoding error:&error]]; // here NSUTF8StringEncoding
if (error) {
NSLog(@"%@", error);
}
} else {
[body appendFormat:@"Content-Disposition:form-data; name=\"%@\"\r\n\r\n", param[@"name"]];
[body appendFormat:@"%@", param[@"value"]];
}
}
[body appendFormat:@"\r\n--%@--\r\n", boundary];
NSData *postData = [body dataUsingEncoding:/*NSASCIIStringEncoding*/NSASCIIStringEncoding]; // here NSUTF8StringEncoding
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://api.vize.ai/v1/classify/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];
}
What's my problem here?