gpt4 book ai didi

ios - 将 cURL 请求(使用 --data-urlencode)转换为 AFNetworking GET 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:12:00 25 4
gpt4 key购买 nike

此问题可能与 AFNetworking 无关,而更多地与构建 NSURLRequest 有关。我正在尝试使用 AFNetworking- 发出下降的 GET 请求-

curl -X GET \
-H "X-Parse-Application-Id: Q82knolRSmsGKKNK13WCvISIReVVoR3yFP3qTF1J" \
-H "X-Parse-REST-API-Key: iHiN4Hlw835d7aig6vtcTNhPOkNyJpjpvAL2aSoL" \
-G \
--data-urlencode 'where={"playerName":"Sean Plott","cheatMode":false}' \
https://api.parse.com/1/classes/GameScore

这是来自 parse.com API https://parse.com/docs/rest#queries-constraints .

但是我不知道怎么写

[AFHTTPClient getPath:parameters:success:failure:]

对于这个请求。 where 子句看起来不像字典,但是此函数仅将字典作为其参数 输入。

最佳答案

该参数需要一个 NSDictionary,它将在 URL 中转换为键/值对。所以 key 很简单,但对于值,您需要在将其设置到字典中之前将其转换为 JSON...

NSDictionary *jsonDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Sean Plott", @"playerName",
[NSNumber numberWithBool:NO], @"cheatMode", nil];

NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];

if (!jsonData) {
NSLog(@"NSJSONSerialization failed %@", error);
}

NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:
json, @"where", nil];

如果我们假设您的客户端是这样配置的(通常您是 AFHTTPClient 的子类,并且可以将这些东西移到里面

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.parse.com/"]];
[client setDefaultHeader:@"X-Parse-Application-Id" value:@"Q82knolRSmsGKKNK13WCvISIReVVoR3yFP3qTF1J"];
[client setDefaultHeader:@"X-Parse-REST-API-Key" value:@"iHiN4Hlw835d7aig6vtcTNhPOkNyJpjpvAL2aSoL"];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];

那么你应该可以调用

[client getPath:@"1/classes/GameScore"
parameters:parameters
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed %@", error);
}];

关于ios - 将 cURL 请求(使用 --data-urlencode)转换为 AFNetworking GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com