简体   繁体   中英

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).

Here is the curl command (with my API token removed) which works:

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):

[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:

{
    "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?

curl encodes the data using URL encoding application/x-www-form-urlencoded . You're using JSON.

Change

[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];

to

[[AFDiffbotClient sharedClient] setParameterEncoding:AFFormURLParameterEncoding];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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