如何将此邮递员请求转换为 iOs 代码
How can transform this Postman Request into iOs Code
我收到了以下 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];
希望有人能帮助我!谢谢!
您正在 post 对图像的 base64 编码字符串进行 json 表示。 postman 请求正在执行具有多部分形式边界的原始二进制文件 post。
您想要更像这里显示的内容
我收到了以下 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];
希望有人能帮助我!谢谢!
您正在 post 对图像的 base64 编码字符串进行 json 表示。 postman 请求正在执行具有多部分形式边界的原始二进制文件 post。
您想要更像这里显示的内容