ios AFHTTPSessionManager 将正文添加到 POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26298593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AFHTTPSessionManager add body to POST
提问by DaSilva
I need too make a post request to my server.
我也需要向我的服务器发出发布请求。
With AFHTTPRequestOperationis very simple just use:
使用AFHTTPRequestOperation非常简单,只需使用:
[request setHTTPBody: [requestBody dataUsingEncoding:NSUTF8StringEncoding]];
However i can't find any example how use the same approach using the AFHTTPSessionManager.
但是,我找不到任何示例如何使用AFHTTPSessionManager使用相同的方法。
Using the method:
使用方法:
[self POST:@"extraLink" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
How i add the the body to the "AFMultipartFormData"?
我如何将主体添加到“AFMultipartFormData”?
Thanks in advace
提前致谢
回答by Rob
As taken from the AFNetworking home page, to create a multipart/form-data
request, you call appendPartWithFileURL
:
取自 AFNetworking主页,要创建multipart/form-data
请求,请调用appendPartWithFileURL
:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
But AFHTTPRequestOperationManager
is deprecated. So, instead, use AFHTTPSessionManager
, for which the syntax of POST
with the constructingBodyWithBlock
is very similar:
但AFHTTPRequestOperationManager
已弃用。因此,请改为使用AFHTTPSessionManager
,其中POST
with 的语法constructingBodyWithBlock
非常相似:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Alternatively, if you want to post a request of the form foo=bar&key=value&...
(i.e. an application/x-www-form-urlencoded
request), you would do something like:
或者,如果您想发布表单请求foo=bar&key=value&...
(即application/x-www-form-urlencoded
请求),您可以执行以下操作:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"foo": @"bar", @"key": @"value"};
[manager POST:@"http://example.com/resources.json" parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
}];
回答by Aman Pathak
Try this other way.
试试这个。
I'm converting my image in data format which is imageData
.
我正在将图像转换为imageData
.
NSData *imageData=nil;
imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"yourimageName"],0.2);
NSMutableDictionary *dict=[NSMutableDictionary new];
[dict setObject:@"user1" forKey:@"param_name1"];
[dict setObject:@"User2" forKey:@"param_name2"];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
manager.responseSerializer = [AFJSONResponseSerializer
serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:@"API NAME" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if(imageData)
{
[formData appendPartWithFileData:imageData name:@"param_name" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
}
} progress:^(NSProgress * _Nonnull uploadProgress)
{
NSLog(@"%@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];