библиотека AFNetworking, у меня mime type стоит image, погуглите какие они бывают и поставте file type, name:@"avatar" - поле на серваке в которое вы данные сохраняете
- (NSURLSessionDataTask *)saveAvatar:(UIImage *)image withCompletion:(CompletionWithErrorBlock)completion {
NSString *apiPath = [NSString stringWithFormat:@"%@/api/avatars", [self currentHost]];
AFHTTPSessionManager *manager = [self getManager];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSString *token = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults]objectForKey:kCurrentToken]];
if(!token.length)
{
completion([NSError errorWithDomain:@"saveAvatar:" code:-33002 userInfo:@{@"Error":@"token from NSUserDefaults is nil"}]);
return nil;
}
NSDictionary *dict = @{@"access_token":token
};
NSNumber *userId = [[NSUserDefaults standardUserDefaults] objectForKey:kAuthorizedUserId];
__block NSString *imageName = [NSString stringWithFormat:@"avatarForUserWithId:%@",userId];
__block typeof(UIImage) *imageInstance = image;
__weak typeof(self) selfInstance = self;
NSURLSessionDataTask *task = [manager POST:apiPath
parameters:dict
constructingBodyWithBlock:^(id _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:imageName mimeType:@"image/jpeg"]; // avarar - name of field in database on server
}
progress:^(NSProgress * _Nonnull uploadProgress)
{
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
completion(nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
[selfInstance reloginWithCompletion:^(NSError *error) {
if (error) {
completion(error);
return;
}
[selfInstance saveAvatar:imageInstance withCompletion:completion];
}];
}];
return task;
}
- (AFHTTPSessionManager *)getManager {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"image/jpeg", @"image/gif", @"image/png", @"multipart/form-data", nil];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *token = [NSString stringWithFormat:@"Bearer %@", [[NSUserDefaults standardUserDefaults]objectForKey:kCurrentToken]];
[manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
return manager;
}