0

In my application, I am performing POST using NSURLSession.

Steps followed:

  1. Setting Header
  2. Setting HTTPBody
  3. Making POST request using NSURLSession.

The code is:

NSDictionary *parameters = @{ @"searchTerm": @"shampoo", @"sort": @"Most Reviewed" };
NSError *error = nil;
NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"SomeURL"]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
request.HTTPBody = postData;

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                if (error) {
                                                    NSLog(@"%@", error);
                                                } else {

                                                    NSLog(@"Pass");
                                                }
                                            }];
[dataTask resume];

Now in custom NSURLProtocol class:

(BOOL)canInitWithRequest:(NSURLRequest *)request {

    if ([request.HTTPMethod isEqualToString:@"POST"]) {
    //here request.HTTPMethod is coming nil
    //Whereas my requirement is to get request.HTTPMethod which got     request parameter.
        return YES;
    }

    return NO;
}

Thanks in advance.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • *request.HTTPBody – shabi naqvi May 30 '17 at 12:22
  • Duplicate of https://stackoverflow.com/questions/36555018/why-is-the-httpbody-of-a-request-inside-an-nsurlprotocol-subclass-always-nil – dgatwood May 30 '17 at 23:52
  • Short answer: By the time you get to a protocol, the data object has been converted into a stream, which you have to read from. – dgatwood May 30 '17 at 23:52
  • Solution is we have to perform [self.request.HTTPBodyStream open]; and then try to access it. – shabi naqvi May 31 '17 at 12:11
  • Possible duplicate of [Why is the HTTPBody of a request inside an NSURLProtocol Subclass always nil?](https://stackoverflow.com/questions/36555018/why-is-the-httpbody-of-a-request-inside-an-nsurlprotocol-subclass-always-nil) – dgatwood Jul 11 '17 at 20:04

1 Answers1

0

IIRC, body data objects get transparently converted into streaming-style bodies by the URL loading system before they reach you. If you need to read the data: Open the HTTPBodyStream object Read the body data from it There is one caveat: the stream may not be rewindable, so don't pass that request object on to any other code that would need to access the body afterwards. Unfortunately, there is no mechanism for requesting a new body stream, either (see the README file from the CustomHTTPProtocol sample code project on Apple's website for other limitations).

Events Wu
  • 1
  • 2