0

I am consuming REST APIs, one of the api is PATCH, i tried to invoke it using following code but it throws error, while same api works in Postman client.

+ (NSURLSessionTask *)callPATCHAPIWithAPIName:(NSString *)apiName andCompletionHandler:(void(^)(id result, NSInteger responseCode, NSError *error))completionHandler
{
    NSString *getURL = @"192.168.1.100/UpdateDeviceInfo/iPhone6/OS 9.2";
    NSURL *URL = [NSURL URLWithString:getURL];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfiguration.timeoutIntervalForRequest = 45.0f;

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];

    // Create Data from request
    NSData *requestData = [NSData dataWithBytes: [@"" UTF8String] length:[@"" length]];

    [request setHTTPMethod:@"PATCH"];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:requestData];

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:
                                  ^(NSData *data, NSURLResponse *response, NSError *error)
                                  {
                                      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;

                                      NSInteger responseCode = [httpResponse statusCode];
                                      NSLog(@"response Code : %ld",(long)responseCode);
                                  }];

    [task resume];
    return task;
}

[Error :Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x14e86d490 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}]

I am passing parameters in query string, not in request body. Though same thing is working in Postman client & Android.

NSPratik
  • 4,714
  • 7
  • 51
  • 81
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
  • I asked a similar question [here](https://stackoverflow.com/questions/48940176/status-code-400-when-converting-postman-request-into-objective-c?noredirect=1#comment84887541_48940176). I'm also having trouble converting a Postman request to Objective-C code. Would you mind taking a look at my question? – fi12 Feb 23 '18 at 02:41
  • Sure @fi12 , will look into it – Dipen Panchasara Feb 23 '18 at 02:44
  • Thanks for your help. – fi12 Feb 23 '18 at 02:45

1 Answers1

4

It seems like you have a whitespace in NSString *getURL = @"192.168.1.100/UpdateDeviceInfo/iPhone6/OS 9.2"

I do not believe whitespaces are supported in url formats! Instead, if whitespaces are necessary, make sure you use getURL = [getURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; in order to handle whitespaces in url strings!

A.J. S.
  • 218
  • 4
  • 15
  • thax for pointing out silly mistake, i didn't even think of it. – Dipen Panchasara Jan 20 '16 at 14:42
  • 1
    Also, that's not a valid URL, you really want to add a scheme (`http://` or `https://`) – jcaron Jan 20 '16 at 14:45
  • @AJ.S I asked a similar question [here](https://stackoverflow.com/questions/48940176/status-code-400-when-converting-postman-request-into-objective-c?noredirect=1#comment84887541_48940176). I'm also having trouble converting a Postman request to Objective-C code. Would you mind taking a look at my question? – fi12 Feb 23 '18 at 02:42