0
votes

i am trying to fetch data in JSON format for the search word 'cancer'.

But i can't figure out how to call the websvice, i tried a few things but they are not working, can anybody help me in this.

Below is the API i should be calling https://api.justgiving.com/docs/resources/v1/Search/FundraiserSearch

Clicking the following URL will get desired data in the browser. https://api.justgiving.com/2be58f97/v1/fundraising/search?q=cancer

apiKey = 2be58f97

Here is the code i am using:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSURL *requestURL = [NSURL URLWithString:@"https://api.justgiving.com/2be58f97/v1/fundraising/search"]; [request setURL:requestURL]; [request setHTTPMethod:@"GET"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];


    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"q\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"%@",searchText] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               NSLog(@"ERROR = %@",error.localizedDescription);
                               if(error.localizedDescription == NULL)
                               {
                                   NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"response >>>>>>>>> %@",returnString);
                               }
                               else
                               {
                                   NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"response >>>>>>>>> %@",returnString);
                               }

                           }];
2
Show some code you have tried. - zaph

2 Answers

0
votes
     -(AFHTTPClient *) getHttpClient{
            AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:[NSURL URLWithString:kBASEURL]];
            httpClient.parameterEncoding = AFJSONParameterEncoding;
            [httpClient setDefaultHeader:@"Accept" value:@"application/json"];
            [httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];

            return httpClient;
        }



        //This is how you should call

-(void) callAPI{

            AFHTTPClient *httpClient = [self getHttpClient];
            NSMutableURLRequest  *request = [httpClient requestWithMethod:@"GET" path:method parameters:queryStrDictionary];// querystringDictionary contains value of all q=? stuff 

            AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                //You have Response here do anything you want.
                [self processResponseWith:JSON having:successBlock andFailuerBlock:failureBlock];

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                //Your request failed check the error for detail
                failureBlock(error);
            }];

            NSOperationQueue *queue = [[NSOperationQueue alloc] init] ;
            [queue addOperation:operation];

        }
0
votes

In your code you setup a multipart/form-data request. While the achievement is creditable, it's not how you talk to the API of the given web service.

In fact, it's simpler:

As you can retrieve from the documentation from that site, "query parameters" go into the URL as a query string: "q=cancer". Then, just specify the Content-Type header as "application/json" - and it should work.

In general, URL query parameters will be prepended to a URL by appending a '?', followed by "non-hierarchical data" comprising the query string and then followed by an optional '#'.

What "non-hierarchical data" means is not exactly specified, but in almost all cases web services require a query string as a list of key/value pairs, whose key and value is separated by a '=', and the pairs are separated by a '&':

param1=value1&param2=value2

Furthermore, in order to disambiguate the query string, say when a value or key itself contains "special characters", like spaces, non-ASCII characters, an ampersand or a equal sign, etc., the query string must be properly "URL encoded" before appended to the url and send to the server.

The details of constructing a URL can be found consulting the corresponding RFC. However, wiki provides a comprehensible definition of the query string in a much more concise form: http://en.wikipedia.org/wiki/Query_string

For further information how to "URL encode" a query string utilizing a handy method or function please read the NSString documentation, stringByAddingPercentEscapesUsingEncoding: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html and Core Foundation: CFURLCreateStringByAddingPercentEscapes: https://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFURLRef/Reference/reference.html

A third party library may make this more convenient, nonetheless you should understand what that API means and how you would have to construct a URL, the HTTP headers and the query string yourself.