1
votes

I want to send a JSON String to server using AFNetworking POST request. Currently I am trying following code

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"POST"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:data];

[request setTimeoutInterval:120];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.requestSerializer = [AFJSONRequestSerializer serializer];

AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)

{        NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error)

{

    NSLog(@"Error: %@", error);

}];

[operation start];`

Nothing is happening. The code neither enters success block nor the failure block. What can be the reason? Is there any alternate way of doing this in AFNetworking 2. NOTE: Request is working perfectly on POSTMAN and returns response in less than 500 ms.

1

1 Answers

0
votes

This is working code of mine

NSDictionary *parameters = [NSDictionary dictionary];//set values here

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

[manager POST:@"http://example.com/api" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //Do success code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //do failure code
}];

This is the function which configure HTTP body AFJSONRequestSerializer

#pragma mark - AFURLRequestSerialization

- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                           withParameters:(id)parameters
                                    error:(NSError *__autoreleasing *)error
{
    NSParameterAssert(request);

    if ([self.HTTPMethodsEncodingParametersInURI containsObject:[[request HTTPMethod] uppercaseString]]) {
        return [super requestBySerializingRequest:request withParameters:parameters error:error];
    }

    NSMutableURLRequest *mutableRequest = [request mutableCopy];

    [self.HTTPRequestHeaders enumerateKeysAndObjectsUsingBlock:^(id field, id value, BOOL * __unused stop) {
        if (![request valueForHTTPHeaderField:field]) {
            [mutableRequest setValue:value forHTTPHeaderField:field];
        }
    }];

    if (parameters) {
        if (![mutableRequest valueForHTTPHeaderField:@"Content-Type"]) {
            [mutableRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        }

        [mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
    }

    return mutableRequest;
}

You are using AFJSONRequestSerializer as request serializer and it is meaning, you are sending JSON string from NSDictionary as HTTP body

[mutableRequest setHTTPBody:[NSJSONSerialization dataWithJSONObject:parameters options:self.writingOptions error:error]];
    }

So you all need to do is make NSDictionary from JSON values, and call as above. No need to make JSON string.