node.js
と を使用して完全な RESTful API を作成しましたmongoDB
。を使用すると、API を介して製品のリストを取得するAFNetworking
ように簡単に実装できますが、レコードを更新する方法を実行するのに問題があります。アイデアは、この UUIDString のレコードがあるかどうかを確認し、レコードを更新する場合です。GET
PUT
ターミナルで cURL コマンドを実行した場合。これはうまくいきます:
Modify with _id value of 6FDCBF3D:
curl -i -X PUT -H 'Content-Type: application/json' -d '{"name": "someName", "someValue": "15"}' http://localhost:3000/iaps/6FDCBF3D/
Objective-c コード:
NSURL *baseURL = [[NSURL alloc] initWithString:@"http://localhost:3000/iaps/"];
// NSString *uuid = [UIDevice currentDevice].identifierForVendor.UUIDString;
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client getPath:@"6FDCBF3D" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *jsonError = nil;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:NSJSONReadingMutableContainers error:&jsonError];
// NSLog(@"Return object: %@", json);
if (json) {
NSString *purchasedPkg = [productIdentifier pathExtension];
if ([purchasedPkg isEqualToString:@"productA"]) {
NSString *oldPkg = [json objectForKey:@"somevalue"];
int newPkgVal = oldPkg.intValue + 1;
NSString *newPkg = [NSString stringWithFormat:@"%d",newPkgVal];
[json setValue:newPkg forKey:@"somevalue"];
}
NSMutableURLRequest *request = [client requestWithMethod:@"PUT" path:@"6FDCBF3D" parameters:nil];
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&jsonError];
AFHTTPRequestOperation *op = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"put success");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"put failed");
}];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];