Reputation: 1655
I need to querying some data from Parse API. The code below is the example of CURL from Parse :
curl -X GET \
-H "X-Parse-Application-Id: XXXXX" \
-H "X-Parse-REST-API-Key: XXXXX" \
-G \
--data-urlencode 'where={"key1":"value1","key2":"value2"}' \
https://api.parse.com/1/classes/ClassName
Then, this is my code to achieve that :
NSDictionary *dictionary = @{@"key1": @"value1", @"key1": @"value2"};
NSError *error = nil;
NSString *jsonString = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
if (!jsonData)
NSLog(@"Got an error: %@", error);
else
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *fullUrl = [NSString stringWithFormat:@"https://api.parse.com/1/classes/ClassName?where=%@", jsonString];
NSURL *url = [NSURL URLWithString:fullUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];
[request addValue:@"XXXXX" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request addValue:@"XXXXX" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSLog(@"Data: %@", data);
NSLog(@"Response: %@", response);
}else{
NSLog(@"Error: %@", error);
}
}];
[task resume];
After executing, i've got some error :
2015-01-16 18:19:57.532 ParseTest[37964:1018046]
Error: Error Domain=NSURLErrorDomain Code=-1002
"The operation couldn’t be completed. (NSURLErrorDomain error -1002.)"
UserInfo=0x7d17d320 {NSUnderlyingError=0x7d051f70 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1002.)"}
What would be the Objective-C equivalent for accomplishing the past CURL code? Or what do you suggest?
Thanks in advance.
Upvotes: 0
Views: 627
Reputation: 7310
NSURLErrorDomain error -1002
means the URL is unsupported. My guess is that you need to URL encode your json for the where
argument. Maybe change making your URL to this:
if (!jsonData)
NSLog(@"Got an error: %@", error);
else
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
jsonString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *fullUrl = [NSString stringWithFormat:@"https://api.parse.com/1/classes/ClassName?where=%@", jsonString];
If this doesn't work, have you tried logging fullUrl
and try to cURL
it?
Upvotes: 1
Reputation: 11598
You need to use a PFQuery
. Information for that can be found in Parse's iOS/OS X Documentation.
Here's some sample code for doing what you're attempting above. It should be a direct replacement for that cURL code:
- (void)runQuery {
PFQuery *query = [PFQuery queryWithClassName:@"ClassName"];
[query whereKey:@"key1" equalTo:value1];
[query whereKey:@"key2" equalTo:value2];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error != nil) {
// Do error handling here
} else {
// You have a valid query result
}
}
];
}
Just don't forget that you need to initialize your Parse connection in your app before you can make calls. That way, you don't need to pass your keys with each request, as you do in your cURL code. You'd do that in your AppDelegate
object's -didFinishLaunchingWithOptions:
method, like so:
#import <Parse/Parse.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize Parse.
[Parse
setApplicationId:@"<App Id>"
clientKey:@"<Client Key>"];
}
Upvotes: 1