user2636368
user2636368

Reputation: 632

UNIRest Objective-C Currency Converter API

I am trying to use the currency converter API from mashape located at https://www.mashape.com/ultimate/currency-convert#!

I am new to Objective-C. I am trying to call the API through this code -

NSDictionary* headers = @{@"X-Mashape-Authorization": @"key"};
NSDictionary* parameters = @{@"amt": @"2", @"from": @"USD", @"to": @"INR", @"accuracy": @"2"};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://exchange.p.mashape.com/exchange/?amt=120&from=usd&to=gbp&accuracy=3&format=json"];
    [request setHeaders:headers];
    [request setParameters:parameters];
}] asJson];

Can someone tell me how I can access the information returned and also how to send the parameter 2 as a number instead of a string.

Upvotes: 2

Views: 1036

Answers (1)

Jędrek Kostecki
Jędrek Kostecki

Reputation: 211

It seems like mashape's APIs are not all standardized to the point of taking parameters from the parameter array - you need to pass them in the setUrl call of your UNIHTTPJsonResponse object.

Also, using async calls when getting data from a remote API like this is a Good Idea.

    NSDictionary* headers = @{@"X-Mashape-Authorization": @"key"};


[[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://exchange.p.mashape.com/exchange/?amt=120&from=usd&to=gbp&accuracy=3&format=json"]; // this is where you want to set your currencies, amounts, etc. 
    [request setHeaders:headers];
    [request setParameters:@{}]; // is this needed? I dunno
}] asJsonAsync:^(UNIHTTPJsonResponse* response, NSError *error) {
    if (error) {
        NSLog(@"%@",[error localizedDescription]);
    } else {

        // here you do all the stuff you want to do with the data you got.
        // like launch any code that actually deals with the data :)

        NSDictionary *currencyResult = [NSJSONSerialization JSONObjectWithData:[response rawBody] options: 0 error: &error];
        NSLog(@"%@", currencyResult);
    }
}];

Upvotes: 2

Related Questions