I'm trying to use Diffbot to parse some URLs into the relevant article portion. They have an "Article API" that allows you to submit one link at a time and receive it back, but for speed I'd prefer to use the Batch API which basically allows you to submit a bunch of Article API requests into one big request and get one big response, instead of one at a time.
Here's what the Batch API described in their documentation (that is oddly behind a login wall):
I'm submitting to the Article API as so:
NSURLRequest *request = [[AFDiffbotClient sharedClient]
requestWithMethod:@"GET"
path:[NSString stringWithFormat:@"article?token=MYTOKEN&fields=url,text,title&url=%@", URL]
parameters:nil];
And it's working perfectly. AFDiffbotClient
is a singleton combined with AFNetworking
to help me make requests easier, and the URL parameter is just the URL of the article I'm looking at. (Perhaps I could be doing that without creating the URL manually, bonus points if anyone could offer tips on that.)
However, with the Batch API, you're supposed to submit (POST) a bunch of these requests as a JSON array. I'm confused how I would go about doing this.
EDIT: I've worked some more on it, and made some progress, but I'm getting a 400 error back. I can't figure out what I'm doing wrong, but I must be along the right path. I'm passing parameters in the POST request with my token and my JSON array, but it still won't work.
[AFDiffbotClient sharedClient].operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
NSMutableArray *DiffbotRequests = [[NSMutableArray alloc] init];
for (NSDictionary *URLAndID in URLsAndIDs) {
NSString *articleURL = [URLAndID objectForKey:@"URL"];
NSDictionary *request = @{@"token": @"TOKEN",
@"fields": @"text,title,url",
@"url": articleURL};
[DiffbotRequests addObject:request];
}
NSError *error;
NSData *DiffbotRequestsJSONData = [NSJSONSerialization dataWithJSONObject:DiffbotRequests options:kNilOptions error:&error];
NSString *DiffbotRequestsJSONString = [[NSString alloc] initWithData:DiffbotRequestsJSONData encoding:NSUTF8StringEncoding];
NSDictionary *parameters = @{@"token": @"TOKEN",
@"batch": DiffbotRequestsJSONString};
[[AFDiffbotClient sharedClient] getPath:@"batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
And here's the response I get back:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0xc2ee4d0 {NSLocalizedRecoverySuggestion=, AFNetworkingOperationFailingURLRequestErrorKey= { URL:
And after that it's just all the URLs I submitted.
EDIT 2: Added an image of the API above.
EDIT 3: Current, unworking code:
[AFDiffbotClient sharedClient].operationQueue.maxConcurrentOperationCount = NSOperationQueueDefaultMaxConcurrentOperationCount;
NSMutableArray *DiffbotRequests = [[NSMutableArray alloc] init];
for (NSDictionary *URLAndID in URLsAndIDs) {
NSString *articleURL = [URLAndID objectForKey:@"URL"];
NSDictionary *request = @{@"token": @"TOKEN",
@"fields": @"text,title,url",
@"url": articleURL};
[DiffbotRequests addObject:request];
}
NSError *error;
NSData *DiffbotRequestsJSONData = [NSJSONSerialization dataWithJSONObject:DiffbotRequests options:NSJSONWritingPrettyPrinted error:&error];
NSString *DiffbotRequestsJSONString = [[NSString alloc] initWithData:DiffbotRequestsJSONData encoding:NSUTF8StringEncoding];
NSDictionary *parameters = @{@"token": @"TOKEN",
@"batch": DiffbotRequestsJSONString};
[[AFDiffbotClient sharedClient] setParameterEncoding:AFJSONParameterEncoding];
[[AFDiffbotClient sharedClient] postPath:@"batch" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", error);
}];
It's informal:
NSURLRequest *request = [[AFDiffbotClient sharedClient] requestWithMethod:@"GET" path: [NSString stringWithFormat:@"article?token=MYTOKEN&fields=url,text,title&url=%@", URL] parameters:nil];
Better way:
NSArray *paramters = @[@"token": @"MYTOKEN",
@"fields": @"url,text,title",
@"url":@"aURL"
]
NSURLRequest *request = [[AFDiffbotClient sharedClient] requestWithMethod:@"GET" path:@"article" parameters:parameters];
The parameters will be URL-encoded into your URL finally seems as your origin one.
If you want POST JSONArray, you should use a POST method and there is a postObject. And also you should set a postObject encode method like:
typedef enum {
AFFormURLParameterEncoding,
AFJSONParameterEncoding,
AFPropertyListParameterEncoding,
} AFHTTPClientParameterEncoding;
For AFNetworking 2.0
Below code shows how you can pass dictionary of key value pairs to an URL
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"token": @"TOKEN"}; // you can add as many parameters as you want it in this dictionary in your case
parameters = nil; // **to show this sample code works** i have set **parameters to nil**
// if you want to sent parameters you can use above code
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"http://maps.google.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With