AFNetworking 3 x-www-form-urlencoded post data

空扰寡人 提交于 2019-12-04 05:12:28

After commenting I finally found the answer to this. Here's my correctly functioning request now, note the addition of

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];

Here's the full code:

NSString *url = [NSString stringWithFormat:@"%@%@",APIBASE,APIUSERENDPOINT];

NSDictionary* parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                          username, @"username",
                          password, @"password",
                          nil
                          ];

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

[manager POST:url parameters:parametersDictionary progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];

try append custom header info ,for example:

[self.requestSerializer setValue:@" application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type];

hope it help for you.

Here . It worked with me . So easy .

 NSDictionary* parametersDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"deviceTokenIOS", @"db487c983ebbe7c2fb066d292bb4318175f54ab27b6b9df7871907e1d0ed62ba",
                                      @"message", @"Hello Dunglt",
                                      nil
                                      ];

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"db487c983ebbe7c2fb066d292bb4318175f54ab27b6b9df7871907e1d0ed62ba", @"deviceTokenIOS", @"Hello Dunglt", @"message", nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];

[manager POST:[NSURL URLWithString:url].absoluteString parameters:dict progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"%@", responseObject);
}
      failure:^(NSURLSessionTask *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }];}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!