0

i'm currently working on an itunes search app. i'm following the iTunes API documentation (https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/) and AFNetworking sample here (https://github.com/AFNetworking/AFNetworking). But i'm getting an error of "too many http redirects". Here's my code:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/search?term=jack+johnson&limit=25"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {
            NSLog(@"%@ %@", response, responseObject);
        }
    }];
    [dataTask resume];

I've also tried:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"https://itunes.apple.com/search?term=jack+johnson&limit=25"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
        return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error.localizedDescription);
        } else {
            NSLog(@"File downloaded to: %@", filePath);
            NSDictionary *result;
            NSData *jSONData = [NSData dataWithContentsOfURL:filePath];  // will block if url is not local!

            if (jSONData) {
                result = [NSJSONSerialization JSONObjectWithData:jSONData options:0 error:NULL];
                NSLog(@"%@", result);
            }
        }
    }];
    [downloadTask resume];

Thanks in advance for answering. ps. im new here :3

  • 1
    Does this answer your question? [How to handle "too many HTTP redirects" Error](https://stackoverflow.com/questions/38702529/how-to-handle-too-many-http-redirects-error) – Raghul Raj Jun 12 '20 at 06:06
  • My peers/classmates were able to make it work. I don't want to ask them what exactly they did though. But they confirmed that the iTunes search api should be working fine. :( – mark kevin cagandahan Jun 12 '20 at 06:56

1 Answers1

0

I had the same problem using Swift with an app named iTunes. Renaming the app or overriding the user agent in the request solves the issue, because Apple some how parses the User-Agent for iTunes:

curl -I --request GET \
--url 'https://itunes.apple.com/search?term=jack%2Bjohnson&limit=25' \
-H 'User-Agent: iTunes/1 CFNetwork/808.3 Darwin/16.3.0'

Returns a HTTP/2 302, to set a custom User-Agent try:

var urlRequest = URLRequest(url: url);
urlRequest.setValue("XYZ", forHTTPHeaderField: "User-Agent")
let (data, _) = try await URLSession.shared.data(for: urlRequest)
Fardage
  • 1
  • 1