簡體   English   中英

如何在Objective-C中模擬此curl語句?

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

我想發布到一個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

這是我在使用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);
}];

當我使用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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM