如何将AFNetworking请求打印为RAW数据 [英] How to print AFNetworking request as RAW data
问题描述
出于调试目的,我想打印整个请求体。我正在使用AFHTTPClient。打印客户端提供一些信息,如标题,但post / get params不存在。
For debuging purposes I'd want to print whole request body. I'm using AFHTTPClient. printing client gives some information, like headers but post/get params are not there.
有没有办法做到这一点?
IS there a way to do it?
推荐答案
内置 - 在AFNetworking工具中
对于AFNetworking 1.x,请使用 AFHTTPRequestOperationLogger
。
对于AFNetworking 2.x,请使用 AFNetworkActivityLogger
。
For AFNetworking 2.x, use AFNetworkActivityLogger
.
这些工具都使用AFNetworking广播的 NSNotification
将请求和响应数据记录到控制台。要显示的信息量是可配置的,并且可以将它们配置为忽略某些操作。
These tools both use the NSNotification
broadcast by AFNetworking to log request and response data to the console. The amount of information to be displayed is configurable, and they can be configured to ignore certain operations.
如果要检查传出请求的正文,请查看 NSURLRequest
的 HTTPBody
参数,这是 AFHTTPRequestOperation
上的属性。
If you want to examine the body of an outgoing request, look at the NSURLRequest
's HTTPBody
parameter, which is a property on your AFHTTPRequestOperation
.
例如,在方法 - [AFHTTPClient getPath:
,在发出请求后,你可以在调试器中输入:
参数:
成功:
失败:]
For example, in the method -[AFHTTPClient getPath:
parameters:
success:
failure:]
, after the request is made, you can type this into the debugger:
po [[NSString alloc] initWithData:request.HTTPBody编码:4]
4是 NSUTF8StringEncoding
,as在NSString.h中定义。
4 is NSUTF8StringEncoding
, as defined in NSString.h.
NSURLRequest
的 HTTPMethod
参数提供方法(GET,POST,PUT等)为 NSString
。
The NSURLRequest
's HTTPMethod
parameter provides the method (GET, POST, PUT, etc.) as an NSString
.
当您的服务器响应时,您的成功完成块将被传递 AFHTTPRequestOperation
对象(默认情况下称为 operation
)。你可以:
When your server responds, your success completion block is passed an AFHTTPRequestOperation
object (called operation
by default). You can:
-
p(int)[[操作响应] statusCode]
到查看状态代码 -
po [[操作响应] allHeaderFields]
查看标题 -
po [operation responseString]
查看响应正文 -
po [operation responseObject]
查看响应对象(如果无法序列化,可能nil
)
p (int)[[operation response] statusCode]
to see the status codepo [[operation response] allHeaderFields]
to see the headerspo [operation responseString]
to see the response bodypo [operation responseObject]
to see the response object (which may benil
if it couldn't be serialized)
这篇关于如何将AFNetworking请求打印为RAW数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!