6

I am using AFNetworking to perform a web request on the VolunteerMatch API. When I perform the request I receive a 200 code, however I do not receive a response. Here is how a typical VolunteerMatch request looks like:

GET /api/call?action=helloWorld&query=... HTTP/1.1
Host: www.volunteermatch.org
Accept-Charset: UTF-8
Content-Type: application/json
Authorization: WSSE profile="UsernameToken"
X-WSSE: UsernameToken Username="acme", PasswordDigest="quR/EWLAV4xLf9Zqyw4pDmfV9OY=",
Nonce="d36e316282959a9ed4c89851497a717f", Created="2003-12-15T14:43:07-0700"

Here is the extended documentation.

Here is my code that I am using for making my request:

 // Create parameters
    NSDictionary* param = @{
                            @"action":@"helloWorld",
                            @"query":@"{\"name\":\"john\"}"
                            };

    // Create Manager
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    // Add Headers
    [manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:[CocoaWSSE headerWithUsername:@"XXXXX" password:@"XXXXXXXXXXX"] forHTTPHeaderField:@"X-WSSE"];

    // Perform Request
    [manager GET:@"http://www.volunteermatch.org/api/call" parameters:param success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSLog(@"HTTP Request URL: %@", [operation.request URL]);
        NSLog(@"HTTP Response Status Code: %ld", [operation.response statusCode]);
        NSLog(@"HTTP Response Body: %@", responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
         NSLog(@"HTTP Request failed: %@", error);
    }];

Here is the response I am getting:

2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Status Code: 200
2015-11-17 16:53:36.476 XXXXXX[85280:5969811] HTTP Response Body: (null)
Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Prad
  • 469
  • 1
  • 8
  • 28
  • Have you tried to issue an identical request outside of the app? Try using an external tool (e.g. https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=en) to check whether the expected body is indeed contained in the response. – kajot Nov 26 '15 at 23:18

3 Answers3

6

i am using one common methods for AFNetworking WS Calling. Uses:

Call WS:

NSDictionary* param = @{
                        @"action":@"helloWorld",
                        @"query":@"{\"name\":\"john\"}"
                        };

[self requestWithUrlString:@"URL" parmeters:paramDictionary success:^(NSDictionary *response) {
    //code For Success
} failure:^(NSError *error) {
   // code for WS Responce failure
}];

Add Two Methods: this two methods are common, you can use these common method in whole project using NSObject class. also add // define error code like...

define kDefaultErrorCode 12345

- (void)requestWithUrlString:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *error))failure {

[self requestWithUrl:stUrl parmeters:parameters success:^(NSDictionary *response) {
    if([[response objectForKey:@"success"] boolValue]) {
        if(success) {
            success(response);
            
        }
    }
    else {
        NSError *error = [NSError errorWithDomain:@"Error" code:kDefaultErrorCode userInfo:@{NSLocalizedDescriptionKey:[response objectForKey:@"message"]}];
        if(failure) {
            failure(error);
        }
    }
} failure:^(NSError *error) {
    if(failure) {
        failure(error);
    }
}];}

and // Set Headers in Below Method (if required otherwise remove)

- (void)requestWithUrl:(NSString *)stUrl parmeters:(NSDictionary *)parameters success:(void (^)(NSDictionary *response))success failure:(void (^)(NSError *))failure {

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];


[manager.requestSerializer setValue:@"WWSE profile=\"UsernameToken\"" forHTTPHeaderField:@"Authorization"];



[manager GET:stUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    if([responseObject isKindOfClass:[NSDictionary class]]) {
        if(success) {
            success(responseObject);
        }
    }
    else {
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        if(success) {
            success(response);
        }
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    if(failure) {
        failure(error);
    }
}];}

For any issues and more Detail please visit AFNetworking


UPDATE
---------- migrating with AFNetworking 3.0 --------- migrate your AFN-2.0 to 3.0 with Some small changes please chaeck here AFN 3.0

Community
  • 1
  • 1
Vvk
  • 4,031
  • 29
  • 51
  • When I perform these requests this is the errors I am receiving this error ```Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'``` – Prad Nov 24 '15 at 06:32
  • okay, please try this Ws in Poster or Rest client. responce is proper or not. i thing responce contain HTMl elements. please check and reply me. – Vvk Nov 24 '15 at 06:52
  • i tested , There are no any responce setted fromWS side. please add Success and failure like{"success": 1} or {"success": 0} – Vvk Nov 24 '15 at 07:07
  • @prnk28 hello friend i can't understand where you facing problems? – Vvk Nov 27 '15 at 04:45
  • @VvkAghera can we use it is as a common NSObject Class? – Jagat Dave Nov 27 '15 at 04:58
  • @JAGAT yes you can use it as a common calling method./singletone class. – Vvk Nov 28 '15 at 05:09
5

Try to integrate AFNetworking latest version 2.0 as guided here & don't forget to add AFSecurityPolicy.h, AFSecurityPolicy.m file. This will support https: requests.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Hemali Luhar
  • 349
  • 1
  • 11
4

You should not first serialize the request and then add new headers. The headers won't end up in the (serialisation of) the request that way. You're probably fine if you reverse that.

ecotax
  • 1,933
  • 17
  • 22
  • I updated code to what you have said I have provided it in the original question however I still receive no response – Prad Nov 24 '15 at 05:06