我必须将包括图像在内的一些数据添加到服务器,并提供以下详细信息:
方法:POST 服务器网址:http://dev.aaaa.com/register
接头: 关键价值观 App_id xxxxx App_key yyyyy enctype multipart / form-data
体:
关键值 data {“code”:“21187”,“fname”:“Manish”,“lname”:“Sharma”} image mypic.jpeg
朋友们请帮我在AFNetworking下用身体和标题形成请求。
感谢大家考虑这个问题并协助我
答案 0 :(得分:0)
UIImage *image = //Your Image.
NSDictionary *params =@{@"data": @{@"code" : @"21187",
@"fname" : @"Manish",
@"lname" : @"lname"
}
};
static AFHTTPRequestOperationManager *afPostManager = nil;
afPostManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL: [NSURL URLWithString:@"http://dev.aaaa.com"]];
[afPostManager.responseSerializer setAcceptableContentTypes:[NSSet setWithObject:@"application/json"]];
[afPostManager.requestSerializer setValue:@"xxxx" forHTTPHeaderField:@"app-id"];
[afPostManager.requestSerializer setValue:@"xxxx" forHTTPHeaderField:@"app-key"];
[afPostManager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"enctype"];
afPostManager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[afPostManager POST:@"register" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[formData appendPartWithFileData:imageData name:title fileName:@"mypic.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"success : %@",responseObject);
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"failed : %@",error.localizedDescription);
}];
试试这个
答案 1 :(得分:0)
NSDictionary *dataDict = <data to send>;
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [NSString stringWithFormat:@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]];
NSLog(@“json = %@", jsonData);
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURL *baseURL = [NSURL URLWithString:@“http://dev.aaaa.com/”];
NSLog(@“base URL = %@", baseURL);
AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL sessionConfiguration:config];
[sessionManager.requestSerializer setTimeoutInterval:500.0];
sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
sessionManager.responseSerializer = responseSerializer;
sessionManager.responseSerializer.acceptableContentTypes = [sessionManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[sessionManager POST:@“register” parameters:dataDict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//-------- here append images as form data -------------------------
[formData appendPartWithFileData:imageData name:@“image” fileName:@“myimage.jpg" mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
progressBlock(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successBlock(task, responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
faliureBlock(task, error);
}];
使用此功能可能会对您有所帮助。