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?
Asked
Active
Viewed 528 times
1
-
Could you please show us some code ? :) – Gabriel Lidenor Dec 22 '16 at 23:12
-
That would be rather tedious, but I'm glad to say I found the issue and am writing it up right now! =) – Minimi Dec 22 '16 at 23:15
2 Answers
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
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