AFHTTPSessionManager将主体添加到POST [英] AFHTTPSessionManager add body to POST
问题描述
我也需要向我的服务器发帖。
I need too make a post request to my server.
使用 AFHTTPRequestOperation 非常简单,只需使用:
With AFHTTPRequestOperation is very simple just use:
[request setHTTPBody: [requestBody dataUsingEncoding:NSUTF8StringEncoding]];
但是我找不到任何使用 AFHTTPSessionManager的相同方法的示例。
However i can't find any example how use the same approach using the AFHTTPSessionManager.
使用方法:
[self POST:@"extraLink" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
} success:^(NSURLSessionDataTask *task, id responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
我如何将主体添加到AFMultipartFormData?
How i add the the body to the "AFMultipartFormData"?
谢谢你推荐
推荐答案
取自AFNetworking 主页,要创建 multipart / form-data
请求,请调用 appendPartWithFileURL
:
As taken from the AFNetworking home page, to create a multipart/form-data
request, you call 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);
}];
但不推荐使用 AFHTTPRequestOperationManager
。因此,请使用 AFHTTPSessionManager
,其中 POST
的语法为 constructBodyWithBlock
非常相似:
But AFHTTPRequestOperationManager
is deprecated. So, instead, use AFHTTPSessionManager
, for which the syntax of POST
with the constructingBodyWithBlock
is very similar:
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);
}];
或者,如果您想发布请求表格 foo = bar& key = value& ...
(即 application / x-www-form-urlencoded
请求),你会做类似的事情:
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:
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);
}];
这篇关于AFHTTPSessionManager将主体添加到POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!