gpt4 book ai didi

ios - 我如何在 Objective-C 中模拟这个 curl 语句?

转载 作者:行者123 更新时间:2023-11-29 12:55:32 28 4
gpt4 key购买 nike

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

这是有效的 curl 命令(删除了我的 API token ):

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 的尝试(同样,删除了 token ):

[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 交互时究竟做错了什么?

最佳答案

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

改变

[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];

[[AFDiffbotClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];

关于ios - 我如何在 Objective-C 中模拟这个 curl 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241470/

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