我收到了以下Postman请求,但工作正常(屏幕截图http://postimg.org/image/s7zm3qhvh/)。但是,当我在iOS中尝试相同时,它将无法正常工作。也许有人可以给我一些信息。
我的Objective-c代码:
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];
我希望有人可以帮助我!谢谢!
答案 0 :(得分:0)
您发布了图像的base64编码字符串的json表示。邮递员请求正在执行具有多部分表单边界的原始二进制文章。
你想要更像这里显示的内容https://stackoverflow.com/a/23517227/96683