1

I'm making web requests using AFNetworking. I subclassed NSURLProtocol and method swizzled it to override the default NSURLSessionConfiguration and add my protocol class to intercept web requests. When canInit is called I try to print the request data, specifically I want the body and query but it always prints null for both. I know the requests are being correctly intercepted they just don't have any body/query data. Does anyone know why this is? How do I go about getting the body data?

Minimi
  • 921
  • 10
  • 29

2 Answers2

3

So after doing some research I found that the apple docs say this about a NSURLRequests body and bodystream:

The receiver will have either an HTTP body or an HTTP body stream, only one may be set for a request. A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.

So, after checking the httpbodystream I found that was where the actual body was being stored. I had to copy the input stream, convert it to data, convert that to string and print. Here's a reference on how I did that:

How to convert NSInputStream to NSString or how to read NSInputStream

Community
  • 1
  • 1
Minimi
  • 921
  • 10
  • 29
0

If you do that in NSURLProtocol, it does not work and stream never will never be opened :

[self.request.HTTPBodyStream open];

You must do that instead :

NSInputStream* stream = self.request.HTTPBodyStream;
[stream open];
SuperCed
  • 53
  • 5