user1227928
user1227928

Reputation: 829

AFNetworking showing unsupported URL for http get operation?

Right now i am using latest AFNetworking library to perform HTTP Get operation but i am getting unsupported URL error message

But when the same, i am hitting in browser i am getting the data

https://abc.xyz.com/status?id={"request":["123"," 456"]}

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:BaseURLWithStatusInformation
      parameters:statusId
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
         }];

so here BaseURLWithStatusInformation is https://abc.xyz.com/status?id=

and statusId is {"request":["123"," 456"]} i.e. a dictionary containing array.

Upvotes: 1

Views: 1021

Answers (2)

mxcl
mxcl

Reputation: 26913

Seems like your parameters should be:

@{@"id": @"{\"request\":[\"123\",\" 456\"]}"}

And your BaseURLWithStatusInformation (presumably now called just BaseURL):

https://abc.xyz.com/status

Since you have a mix of query string and JSON there. AFNetworking won't do both. I believe.

Suggested code:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
id url = @"https://abc.xyz.com/status";
id params = @{@"id": @"{\"request\":[\"123\",\" 456\"]}"};
[manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Edit: it's probably worth checking what the server actually wants, this mix of JSON and query string format is a little strange. Possibly your parameters should instead be:

@{@"id": @{@"request":@[@"123", @"456"]}}

Upvotes: 2

Alexander Perechnev
Alexander Perechnev

Reputation: 2837

I think the GET method is not the best way to pass array parameters to server. At least because the URL has limitations in it's length.

Anyway, your URL should look like https://abc.xyz.com/status without any parameters at the end. AFNetworking will add all parameters by itself.

And your parameters should look like:

{"id":{"request":["123"," 456"]}}

Upvotes: 0

Related Questions