Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volunteermatch API Objective C

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)
like image 291
Prad Avatar asked Nov 17 '15 22:11

Prad


2 Answers

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

like image 71
Vvk Avatar answered Sep 21 '22 16:09

Vvk


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.

like image 22
Hemali Luhar Avatar answered Sep 20 '22 16:09

Hemali Luhar