I got the following Postman request which works fine (Screenshot http://postimg.org/image/s7zm3qhvh/). But when i try the same in iOS it will not work. Maybe someone can give me some information why.
My Objective-c Code:
UIImage *yourImage= [UIImage imageNamed:@"login-main-bg.png"];
NSString *imageString = [UIImagePNGRepresentation(yourImage) base64Encoding];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
imageString, @"image",
nil];
NSError *error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"%@",[error localizedDescription]);
}
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://server.website.net/api/collaboration/ImageTest"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonData];
//print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
I hope someone can help me! Thank you!