I have a web service that I am trying to get some data from. The final URL should look something like this:
http://server.com/services/v2/item/3?....ect
The value 3 in the above URL is dynamic and I need to change it base on a user action. I have all that part down and for the URL I just put the parameters into an NSDictionary
like all my otherwise services.
The only difference is this web service doesn't use a key for the value, like the other web services do. So I just put the value into NSDictionary without the key.
After making the request the final URL looks like this:
http://server.com/services/v2/item/?3.....ect
The base URL I am using: http://server.com/services/v2/item/
I just need a value in front of it with the question mark after the value, not before.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"http://server.com/services/v2/item/%d?",myValue] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
Instead of passing a NSDictionary
to parameters
, pass nil
. Then construct the URL with parameters with stringWithFormat
, and pass it to GET
Usually base URL is set to you api endpoint root, so in your case it should be http://server.com/services/v2 .
Also it's ok to pass parameters (the ID of item in your case) via url string and mix it with parameters
dictionary.
The method to fetch an item may look like this
...
// Set a base URL to your session/operations manager
self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL urlWithString:@"http://server.com/services/v2"]];
...
- (NSURLDataTask *)fetchItemWithID:(NSString *)ID parameters:(NSDictionary *)parameters success:... failure:...
{
NSString *urlPathString = [NSString stringWithFormat:@"item/%@", ID);
return [self.sessionManager GET:urlPathString parameters:parameters success:success failure:failure];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.