2016-08-19 14 views
1

{"token": "asdfwrwer234234d"}をhttp bodyとして設定する必要があります。AFNetworking 3.0で本文を設定できません

self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 

[self.requestSerializer setValue:Token forHTTPHeaderField:@"Token"]; 

[self POST:[NSString stringWithFormat:@"%@2/9/1",BaseURL] parameters:nil 
    progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 

    completion(responseObject,YES); 

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 
    completion(error,NO); 

}]; 
+0

そして、あなたが持っている問題はありますか? – pableiros

+0

http bodyとしてhttp bodyとして設定していないので、HttpBodyを設定する方法が必要です。 – ManiaChamp

答えて

0

それは私の問題を解決:

NSDictionary *body = @{@"token":Token}; 
NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error]; 
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 


AFHTTPResponseSerializer *responseManager = [AFHTTPResponseSerializer serializer]; 

responseManager.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 

manager.responseSerializer = responseManager; 

NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%@2/9/1",BaseURL] parameters:nil error:nil]; 

req.timeoutInterval= [[[NSUserDefaults standardUserDefaults] valueForKey:@"timeoutInterval"] longValue]; 
[req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]]; 


[[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 

    if (!error) { 
     NSError* error; 
     NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject 
                  options:kNilOptions 
                   error:&error]; 

    } else { 
     NSLog(@"Error: %@, %@, %@", error, response, responseObject); 
    } 
}] resume]; 
関連する問題