Search code examples
iosobjective-cunirestmashape

xCode - Mashape API - Unirest


i need some help from you. I found a API on MaShape for Metascore but i just can't get it to work. I used Cocoapod to download Unirest framework and copy pasted the code snippet from Mashape

NSDictionary* headers = @{@"X-Mashape-Authorization": @"wZrjWIiAsqdSLGIh3DQzrKoZ5Y3wlo6E"};
NSDictionary* parameters = @{@"title": @"The Elder Scrolls V: Skyrim", @"platform": 1, };

UNIHttpJsonResponse* response = [[UNIRest post:^(UNIBodyRequest* request) {
  [request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];

  [request setHeaders:headers];
  [request setParameters:parameters];
}] asJson];

It gave me a bunch of errors and i fixed it to be like this:

NSDictionary* headers = @{@"X-Mashape-Authorization": @"wZrjWIiAsqdSLGIh3DQzrKoZ5Y3wlo6E"};
    NSDictionary* parameters = @{@"title": @"The Elder Scrolls V: Skyrim", @"platform": @"1", };

    UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
        [request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];

        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJson];

but whenever i go and debug the code and i look inside the response it's empty as if the api didn't work. Can you guys tell me what i'm doing wrong?

Thanks


Solution

  • Your (fixed) code snippet looks fine (the first one was indeed erroneous), and you should be able to print the result like this:

    UNIHTTPJsonResponse *response = [[UNIRest post:^(UNISimpleRequest *request) {
        [request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
    
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJson];
    
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:nil];
    NSLog(@"Response status: %ld\n%@", (long) response.code, json);
    

    But rather than doing a synchronous call, I would also suggest you to switch to the asynchronous way, as well as checking for any error during the process and the JSON parsing:

    [[UNIRest post:^(UNISimpleRequest *request) {
        [request setUrl:@"https://byroredux-metacritic.p.mashape.com/find/game"];
        [request setHeaders:headers];
        [request setParameters:parameters];
    }] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
        if (error) {
            // Do something with the error
        }
    
        NSError *jsonError;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response.rawBody
                                                             options:kNilOptions
                                                               error:&jsonError];
        if (jsonError) {
            // Do something with the error
        }
    
        NSLog(@"Async response status: %ld\n%@", (long) response.code, json);
    
        // Unirest also provides you this which prevents you from doing the parsing
        NSLog(@"%@", response.body.JSONObject);
    }];