26

I am making a library to get response from a particular URL with specified data and method type. For this, I am making a request with url. But when I set its method type, it shows an exception of unrecognized selector send in [NSURLRequest setHTTPMethod:] I am setting it as

[requestObject setHTTPMethod:@"GET"];

Tell me what could be the problem. Also provide me the code if you have.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Sanchit Paurush
  • 6,114
  • 17
  • 68
  • 107

4 Answers4

82
NSMutableURLRequest *request = 
[NSMutableURLRequest requestWithURL:[NSURL 
            URLWithString:serverAddress] 
            cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                                   timeoutInterval:10
 ];

[request setHTTPMethod: @"GET"];

NSError *requestError = nil;
NSURLResponse *urlResponse = nil;


NSData *response1 =
        [NSURLConnection sendSynchronousRequest:request
                         returningResponse:&urlResponse error:&requestError];
CuriousGeorge
  • 7,120
  • 6
  • 42
  • 74
12

NSString *getString = [NSString stringWithFormat:@"parameter=%@",yourvalue];
NSData *getData = [getString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *getLength = [NSString stringWithFormat:@"%d", [getData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https:yoururl"]];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getData];
self.urlConnection = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
NSAssert(self.urlConnection != nil, @"Failure to create URL connection.");
// show in the status bar that network activity is starting [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • 1
    @simpleBob Didn't work for me either... I got it working fine for POST, but not GET. Then I checked the doco for `setHTTPBody:` which states for the data parameter "The new request body for the receiver. This is sent as the message body of the request, as in an HTTP POST request." – Steph Sharp May 22 '13 at 01:56
  • @StephSharp I use something like this, maybe it helps you: `NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:WS_URL]]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];` – Daniel May 22 '13 at 14:22
  • The URL should just be the URL with parameters. Something like `http://my.URL.com/test?param1=1&param2=2` – Daniel May 22 '13 at 14:26
  • @simpleBob Thanks for the code, that's nearly exactly what I ended up doing! – Steph Sharp May 23 '13 at 05:57
  • Typo: `[request setValue:postLength...` should be `[request setValue:getLength...` – clozach Jul 24 '13 at 18:03
2

Simply call and use:

(void)jsonFetch{

    NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/us/rss/topaudiobooks/limit=10/json"];

    NSURLSession *session = [NSURLSession sharedSession];

    NSURLSessionDataTask *data = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSError *erro = nil;

        if (data!=nil) {

            NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&erro ];

            if (json.count > 0) {

                for(int i = 0; i<10 ; i++){

                    [arr addObject:[[[json[@"feed"][@"entry"] objectAtIndex:i]valueForKeyPath:@"im:image"] objectAtIndex:0][@"label"]];

                }

            }
        }
        dispatch_sync(dispatch_get_main_queue(),^{

            [table reloadData];
        });
    }];

    [data resume];
}
RBT
  • 24,161
  • 21
  • 159
  • 240
Slimshady
  • 21
  • 3
2

Make sure your requestObject is of type NSMutableURLRequest.

Imran Raheem
  • 890
  • 8
  • 25