Post Request to alchemy API in iOS8

207 views Asked by At

Im trying to use the API call TextGetRankedNamedEntities http://www.alchemyapi.com/api/entity/textc.html#rtext

Here is my relevant code:

NSString *data = @"Data I want analyzed";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"result.txt"];
[data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];

//I have verified the data is in the file

NSString *queryString = [NSString stringWithFormat:@"http://access.alchemyapi.com/calls/text/TextGetRankedNamedEntities?apikey=MY_API_KEY&showSourceText=1"];

NSMutableURLRequest *theRequest=[NSMutableURLRequest
                           requestWithURL:[
                               NSURL URLWithString: queryString]
                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                   timeoutInterval:60.0];
    //Set request method to post
    [theRequest setHTTPMethod:@"POST"];
    NSString *post = [NSString stringWithFormat:@"&text=%@", appFile];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
    [theRequest setHTTPBody:postData];

my result for this request is:

<?xml version="1.0" encoding="UTF-8"?>
<results>
    <status>OK</status>
    <usage>By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html</usage>
    <url></url>
    <language>english</language>
    <text>/var/mobile/Containers/Data/Application/6C00A4F8-4DEE-44F2-9DBF-7568CEF72054/Documents/result.txt</text>
    <entities>
    </entities>
</results>

It appears that the data that is being send is the file path and not that actual contents of the file.

EDIT: Solved, Leaving the question for possible feedback and best practices for using HTTTP requests in iOS as I am new to both.

1

There are 1 answers

0
Jim Gorski On

Change:

NSString *post = [NSString stringWithFormat:@"&text=%@", appFile];

To:

NSString *post = [NSString stringWithFormat:@"&text=%@", data];

It didn't occur to me to try this before posting the question because I had previous tried to do this but at that point I was sending the request with the GET method not the POST method.