1
votes

I am trying to implement an Objective C program to interface with activeCollab 3 beta and am having some issues. I can enter the url that the NSLog outputs in the browser and it works just fine pulling the xml for all the projects and it is not wanting to work for me when I try to access it via this program, it is giving me a HTTP 403 error. I am new to Objective C and am doing this as a learning experience so some code may be redundant. Thanks in advance for any help. The import is surrounded in angle brackets but will cause it to be hidden on StackOverflow so I have placed it in quotes

#import "Foundation/Foundation.h"

int main (int argc, const char * argv[]) {

    NSString *token = @"my-token"; 
    NSString *path_info = @"projects";
    NSString *url = @"http://my-site/api.php?";

    NSString *post = [[NSString alloc] initWithFormat:@"path_info=%@&auth_api_token=%@",path_info, token];
    NSLog(@"Post: %@", post);

    NSString *newRequest;
    newRequest = [url stringByAppendingString:post];

    NSLog(@"Path: %@", newRequest);

    NSData *postData = [newRequest dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    [request setURL:[NSURL URLWithString:newRequest]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:postData];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

    NSString *returnData = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
    NSLog(@"%@", returnData);

    //printf("Print line");
    return 0;
}
1

1 Answers

0
votes

Your request headers are restricting and unnecessary, and for AC API you want a GET request rather than POST

int main (int argc, const char * argv[]) 
{

    NSString *requestString = [[NSString alloc] initWithFormat:@"path_info=%@&auth_api_token=%@", path_info, token];
    NSLog(@"Post: %@", requestString);

    NSString *newRequest;
    newRequest = [url stringByAppendingString: requestString];

    NSLog(@"Path: %@", newRequest);

    NSData *postData = [newRequest dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion: YES];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

    NSLog(@"Data: %@", postData);

    [request setURL: [NSURL URLWithString:newRequest]];
    [request setHTTPMethod: @"GET"];

    NSURLResponse *response;

    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &err];
    NSString *returnData = [[[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding] autorelease];
    NSLog(@"Return: %@", returnData);

    //printf("Print line");
    return 0;
}