最近浏览发现现在有时会使用
form-data
格式提交数据,其实和提交json
差不多,但还是有点不同。今天试了一把,这种提交方法现在不常见了,大多使用json
的比较多,至此,记录下。
1、先来演示提交图片+键值对
当然了,我是使用AFNetWorking框架
//需要提交的数据
NSData *data_f = UIImageJPEGRepresentation(zImage, 0.8);
NSData *data_s = UIImageJPEGRepresentation(fImage, 0.8);
NSDictionary * dic = [NSMutableDictionary new];
[dic setValue:@"value1" forKey:@"key1"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
//配置AF
AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
[manage.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
manage.requestSerializer = [AFHTTPRequestSerializer serializer];
manage.responseSerializer = [AFHTTPResponseSerializer serializer];
manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
[manage POST:@"提交的URL" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//当提交一张图片或一个文件的时候 name 可以随便设置,服务端直接能拿到,如果服务端需要根据name去取不同文件的时候,则appendPartWithFileData 方法中的 name 需要根据form的中的name一一对应
[formData appendPartWithFormData:jsonData name:@"UploadData"];
[formData appendPartWithFileData: data_f name:@"photoF" fileName:@"a.jpg" mimeType:@"image/jpeg"];
[formData appendPartWithFileData: data_s name:@"photoS" fileName:@"b.jpg" mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
2、下面来看一种只上传键值对
/**
form-data请求接口
@param url 请求地址
@param parameters 请求参数 传字典就行,所有的键值对都传进来
@param success 成功
@param failure 失败
*/
+ (void)p_postRequestUrl:(NSString *)url
parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(id errorLog))failure {
//配置AF
AFHTTPSessionManager *manage = [AFHTTPSessionManager manager];
[manage.requestSerializer setValue:@"application/form-data" forHTTPHeaderField:@"Content-Type"];
manage.requestSerializer = [AFHTTPRequestSerializer serializer];
manage.responseSerializer = [AFHTTPResponseSerializer serializer];
manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/json", @"text/javascript",@"text/plain", nil];
[manage POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
for (NSString *key in [parameters allKeys]) {
[formData appendPartWithFormData:[[parameters objectForKey:key] dataUsingEncoding:NSUTF8StringEncoding] name:key];
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[SVProgressHUD dismiss];
NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
success(resultDic);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error.description);
}];
}
总结:以上就是使用from-data进行参数提交的,比较特殊点跟之前json提交,上边两个方法的区别是一个传图片一个不传图片,不传图片的小小封装了下,直接传字典进来就行,内部自动转换。
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
另外附加上json请求格式
现在主流还是这种方式,比较方便
ps:直接复现传参数就可以使用
+ (void)zc_postRequestUrl:(NSString *)url
parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(id errorLog))failure {
// 获得请求管理者
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
session.requestSerializer = [AFHTTPRequestSerializer serializer];
// 设置请求格式
session.requestSerializer = [AFJSONRequestSerializer serializer];
[session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
session.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];
[SVProgressHUD show];
[session POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSDictionary *resultDic = responseObject;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error);
}];
}
网友评论