node.js
我使用and创建了一个完整的 RESTful API mongoDB
。使用AFNetworking
,我可以轻松地实现GET
通过 API 检索产品列表,但是我在执行PUT
更新记录的方法时遇到了麻烦。这个想法是检查是否有此 UUIDString 的记录,如果有更新记录。
如果我在终端中运行 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);
}];