0

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What errors are you getting? What is the actual outcome and what is the expected outcome. Without details people will have to guess at the problem. – Robotic Cat Jan 08 '15 at 01:27
  • The problem is the only information i have are these. The other developer provided me only this. I don't get an error or anything back that was the reason why i asked this question. I read through other stackoverflow posts but they can't help. I thought someone will see the problem. But thanks for randomly voting it down! – tobe.bryant Jan 08 '15 at 02:44

1 Answers1

0

You're posting a json representation of a base64 encoded string of your image. The postman request is doing a raw binary post with multipart form boundaries.

You want something more like what is shown here https://stackoverflow.com/a/23517227/96683

Community
  • 1
  • 1
jsd
  • 7,673
  • 5
  • 27
  • 47
  • thanx man that helps me! I will let you know if this fixed my issue! – tobe.bryant Jan 08 '15 at 02:45
  • @jsd, I asked a [similar question](https://stackoverflow.com/questions/48940176/status-code-400-when-converting-postman-request-into-objective-c) here. I'm also having trouble converting a Postman request to Objective-C code. Would you mind taking a look at my question? – fi12 Feb 23 '18 at 02:21