AFNetworking - 做一个HTTP POST到REST,发送一个JSON Base64string图像

问题描述:

我想发送一个base64编码的图像之间的一些其他字符串使用http POST和AFNetworking。我试图发送参数一个NSDictionary:AFNetworking - 做一个HTTP POST到REST,发送一个JSON Base64string图像

NSMutableDictionary *info = [NSMutableDictionary dictionary]; 
    [info setValue:filedata forKey:@"filedata"]; 
    [info setValue:comments forKey:@"comments"]; 
    [info setValue:username forKey:@"username"]; 
    [info setValue:@"jpg" forKey:@"filetype"]; 
    [info setValue:filename forKey:@"filename"]; 

,并使用AFNetworking POST:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

    [manager POST:@"https://myurl.com/fileupload" parameters:info progress:nil success:^(NSURLSessionTask *task, id responseObject) { 
     NSLog(@"JSON: %@", responseObject); 
    } failure:^(NSURLSessionTask *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

但是我不知道还有什么在这一点上做的。我错过了什么?谢谢

您可以用下面的例子

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 

[manager POST:@"https://myurl.com/fileupload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
    [formData appendPartWithFileData:imageData 
           name:@"key name for the image" 
          fileName:photoName mimeType:@"image/jpeg"]; 
} success:^(NSURLSessionDataTask *task, id responseObject) { 
    NSLog(@"Response: %@", responseObject); 
} failure:^(NSURLSessionDataTask *task, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
+0

谢谢你,但是我需要上传所有的值。 '(“username =%@&filename =%@&filetype = jpg&filedata =%@&comments =%@”)'我如何包含这些参数? – McCadi

+0

检查此示例https://*.com/questions/24747809/using-afnetworking-to-post-both-text-and-multiple-images-to-google-blobstore – casillas