AFNetworking 3 x-www-form-urlencoded post data
Asked Answered
M

3

7

I'm trying to post data with x-www-form-urlencoded body. Posting via postman, it is ok Posting via postman, it is ok

But i cant do it via afnetworking 3. Here is my code

NSDictionary *parameters = @{@"login"   : email,
                             @"password": password};

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                   options:0
                                                     error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

self.requestSerializer = [AFJSONRequestSerializer serializer];

NSString *urlString = [NSString stringWithFormat:@"%@/%@", HTTPBaseRequestURL, appendLoginUrl];

NSLog(@"URL %@\njsonString %@", urlString, jsonString);


[self POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    [formData appendPartWithFormData:jsonData name:@"data"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {        
    onSuccess(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSString *errorDescription = [NSError serverErrorMessageFromData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey]];
    NSInteger statusCode = [NSHTTPURLResponse errorCode:(NSHTTPURLResponse*)task.response];

    NetworkRequestError *requestError = [[NetworkRequestError alloc] initWithType:
                                         (NSHTTPURLResponse*)task.response ? NetworkRequestErrorTypeServerError : NetworkRequestErrorTypeNoConnection
                                                                      description:
                                         (NSHTTPURLResponse*)task.response ? errorDescription : nil];
    requestError.statusCode = statusCode;

    NSLog(@"Error from server: %@, status code = %ld, error type = %lu", requestError.errorDescription, (long)requestError.statusCode, (unsigned long)requestError.type);
    onFailure(requestError);
}];

Please, help me to understand how to correctly do this. Thanks!

Methacrylate answered 6/2, 2016 at 9:39 Comment(1)
I'm having the same issue, have you found an answer you can share?Summons
S
9

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);
}];
Summons answered 19/4, 2016 at 17:17 Comment(1)
Why did you set content-type value for requestSerializer and then rewrite it again by replacing whole serializer with a completely new one?Brochure
A
1

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.

Avail answered 8/4, 2016 at 8:34 Comment(0)
T
0

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);
      }];}
Tellurium answered 30/10, 2016 at 6:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.