【问题标题】:How do I emulate this curl statement in Objective-C?如何在 Objective-C 中模拟这个 curl 语句?
【发布时间】:2014-01-20 18:29:24
【问题描述】:

我想发布到一个 API(使用 JSON),我可以通过 curl 很好地通过 bash 脚本来完成它,但是我在 Objective-C 中完成同样的任务时遇到了很多麻烦(我正在使用 AFNetworking,但不是必须的)。

这里是 curl 命令(删除了我的 API 令牌):

curl -d 'token=...' -d 'batch=[{"method":"GET","relative_url":"/api/article?token=...%26url=http://www.macrumors.com/2014/01/12/your-verse-ipad-ad/"}]' http://diffbot.com/api/batch

这是我在 Objective-C 中使用 AFNetworking 的尝试(再次删除了令牌):

[AFDiffbotClient sharedClient].operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
NSMutableArray *individualRequests = [[NSMutableArray alloc] init];

for (NSDictionary *URLAndID in URLsAndIDs) {
    NSString *articleURL = [URLAndID objectForKey:@"URL"];
    NSString *requestURL = [NSString stringWithFormat:@"/api/article?token=...&fields=text,title,url&url=%@", articleURL];

    [individualRequests addObject:@{@"method": @"GET",
                                    @"relative_url": requestURL}];
}

NSError *error;
NSData *individualRequestsJSONData = [NSJSONSerialization dataWithJSONObject:individualRequests options:kNilOptions error:&error];
NSString *individualRequestsJSONString = [[NSString alloc] initWithData:individualRequestsJSONData encoding:NSUTF8StringEncoding];

NSDictionary *parameters = @{@"token": @"...",
                             @"batch": individualRequestsJSONString};

[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];
[[AFDiffbotClient sharedClient] postPath:@"http://diffbot.com/api/batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];

当我使用 Charles 监控我的 HTTP 流量并选择“JSON 文本”时,它声称我要发送到 API:

{
    "token": "...",
    "batch": "[{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/www.macrumors.com\\\/2014\\\/01\\\/12\\\/your-verse-ipad-ad\\\/\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/gigaom.com\\\/2013\\\/08\\\/14\\\/honest-chromecast-review\\\/\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/www.theverge.com\\\/2013\\\/8\\\/14\\\/4622122\\\/oldest-board-game-tokens-found-turkey\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/mobile.slate.com\\\/articles\\\/business\\\/moneybox\\\/2013\\\/08\\\/microsoft_ceo_steve_ballmer_retires_a_firsthand_account_of_the_company_s.single.html?original_referrer=http%3A%2F%2Ft.co%2FyOO5N2OQxZ&utm_campaign=Buffer&utm_content=buffer47791&utm_medium=twitter&utm_source=buffer\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/gigaom.com\\\/2013\\\/08\\\/27\\\/whos-your-new-mobile-carrier-how-bout-wi-fi\\\/\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/www.bloomberg.com\\\/news\\\/2013-11-10\\\/apple-said-developing-curved-iphone-screens-enhanced-sensors.html\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/9to5mac.com\\\/2013\\\/10\\\/04\\\/itunes-radio-launch-in-canada-imminent-as-apple-seeks-programmers\\\/\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/gizmodo.com\\\/5926728\\\/sony-smartwatch-review-maybe-the-worst-thing-sony-has-ever-made\"},{\"method\":\"GET\",\"relative_url\":\"\\\/api\\\/article?token=...&fields=text,title,url&url=http:\\\/\\\/njnewscommons.org\\\/the-news-in-jersey-august-27-2013-obama-and-the-future-of-investigative-journalism\\\/\"}]"
}

这显然是错误的。

我在与 API 交互方面到底做错了什么?

【问题讨论】:

    标签: ios objective-c post curl afnetworking


    【解决方案1】:

    curl 使用 URL 编码 application/x-www-form-urlencoded 对数据进行编码。您正在使用 JSON。

    改变

    [[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];
    

    [[AFDiffbotClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多