Question

I am attempting the use the UNIRest API to run this get request in an iPhone application

https://api.guildwars2.com/v1/guild_details.json?guild_name=The%20Legacy

The code I am running is this

NSDictionary* headers = @{@"accept": @"application/json"};
NSDictionary* parameters = @{@"guild_name": @"The Legacy"};

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://api.guildwars2.com/v1/guild_details.json"];
    [request setHeaders:headers];
    [request setParameters:parameters];
}] asJson];

NSDictionary *guildInformation = response.body.JSONObject;

NSLog(@"response length: %lu", (unsigned long)[guildInformation.allValues count]);

for(NSString *key in [guildInformation allKeys]) {
    NSLog(@"key: %@     object: %@", key, [guildInformation objectForKey:key]);
}

I had hoped the for loop would display the response. But it seems I get no response at all when you see that the only output is,

response length: 0

I don't know the UNIRest API well enough to fix this and cannot find any good documentation for it. What am I doing wrong?

Was it helpful?

Solution

The problem seems to be that the parameters values are not correctly encoded.

As a fast workaround you can simply pass the entire constructed URL.

UNIHTTPJsonResponse* response = [[UNIRest post:^(UNISimpleRequest* request) {
    [request setUrl:@"https://api.guildwars2.com/v1/guild_details.json?guild_name=The%20Legacy"];
    [request setHeaders:@{@"accept": @"application/json"}];
}] asJson];

Probably the space in @"The Legacy" doesn't translate to "The%20Legacy", will do a test case before adding issue to https://github.com/Mashape/unirest-obj-c

UPDATE

Only while I was adding a TestCase for spaced values (which do work correctly) I spotted that you where using POST while you should have used GET.

UNIHTTPJsonResponse* response = [[UNIRest get:^(UNISimpleRequest* request) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top