如何为AFHTTPSessionManager设置HTTPBody?

时间:2016-01-10 16:48:38

标签: ios objective-c xcode7

如何为HTTPBody对象和POST请求设置Json格式的AFHTTPSessionManager

现在我有:

[self.requestManager.requestSerializer setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[self.requestManager POST:@"/api/register/"...

我收到错误:

  

请求失败:禁止(403)

我也试过了:

self.requestManager.requestSerializer = [AFJSONRequestSerializer serializer];

1 个答案:

答案 0 :(得分:0)

==> afnetworking 3.0 Migration: how to POST with headers and HTTP Body

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
    [manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
    [manager setResponseSerializer:[AFJSONResponseSerializer serializer]];

    NSDictionary *parameters = @{@"test": @"this is a test"};

    [manager POST:@"http://www...t" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        NSLog(@"%@",[responseObject description]);

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        NSLog(@"%@",[error localizedDescription]);
    }];