简体   繁体   English

如何在Objective-C中模拟此curl语句?

[英]How do I emulate this curl statement in Objective-C?

I want to post to an API (with JSON), and I'm able to do it via bash scripts fine via curl , but I'm having a great deal of trouble accomplishing the same task in Objective-C (I'm using AFNetworking, but don't have to be). 我想发布到一个API(带有JSON),并且我可以通过curl bash脚本来完成它,但是在Objective-C中完成相同的任务时遇到了很多麻烦(我正在使用AFNetworking,但不必如此)。

Here is the curl command (with my API token removed) which works: 这是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

And here's my attempt in Objective-C using AFNetworking (again, with token removed): 这是我在使用AFNetworking的Objective-C中的尝试(同样,删除了令牌):

[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);
}];

And when I monitor my HTTP traffic using Charles and select "JSON Text", here is what it claims I'm sending to the API: 当我使用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\\\/\"}]"
}

Which is apparently wrong. 这显然是错误的。

What exactly am I doing wrong with interacting with the API? 与API交互时,我到底在做什么错?

curl encodes the data using URL encoding application/x-www-form-urlencoded . curl使用URL编码application/x-www-form-urlencoded对数据进行编码。 You're using JSON. 您正在使用JSON。

Change 更改

[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];

to

[[AFDiffbotClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM