AFNetworking2 send parameter as query string in POST request?

点点圈 提交于 2019-12-25 06:40:02

问题


I need to send query string in URL as well as JSON in body while making POST request.To send query string in url i override HTTPMethodsEncodingParametersInURI property as suggested in this thread on SO like

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:fullUrl]];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];

        manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithArray:@[@"POST", @"GET", @"HEAD", @"PUT", @"DELETE"]];



        [manager POST:fullUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
 }]; 

but it seems doing this add my JSON data into URL as well, hence my request is not executed on server.But i need to send only 'command' parameter into url and some JSON data into request body. my fullUrl string is looks like http://some_ip_address/config?command=some_command and param is NSDictionary object. Note that there is a parameter in fullUrl i.e. command and i also send a NSDictionary object as param in

[manager POST:fullUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     }]; 

Edit2: I also tried with NSURLSession

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://my_server_ip/config?command=plugs"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSError *error;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }];
    [postDataTask resume];

but get status code: 400 in NSURLResponse.

Edit3: someone suggested to subclass AFHTTPRequestSerializer class in this SO thread so i tried with

@implementation CustomAFHTTPRequestSerializer

-(id)init
{
    self = [super init];
    return self;
}
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
                                 URLString:(NSString *)URLString
                                parameters:(NSDictionary *)parameters
                                     error:(NSError *__autoreleasing *)error
{
    NSString* encodedUrl = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSError *error1;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error1];
    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
     return request;
}
@end

And assign it as requestSerializer of operationManager

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.requestSerializer = [[CustomAFHTTPRequestSerializer alloc] init];

But still get Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)

Please help me guys, Any help would be highly appreciated.

来源:https://stackoverflow.com/questions/33369987/afnetworking2-send-parameter-as-query-string-in-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!