Ios 如何在发送前记录JSON

Ios 如何在发送前记录JSON,ios,objective-c,cocoa-touch,afnetworking,Ios,Objective C,Cocoa Touch,Afnetworking,这是我上传文件的POST请求。在实际发送之前,我如何记录发送到服务器的JSON NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:twitid forKey:@"twit"]; [dict setObject:hash forKey:@"hash"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]

这是我上传文件的POST请求。在实际发送之前,我如何记录发送到服务器的JSON

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:twitid forKey:@"twit"];
[dict setObject:hash forKey:@"hash"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
if ([mediaFile isKindOfClass:[UIImage class]])
{
   imageData = UIImageJPEGRepresentation(mediaFile, 0.5); 
}

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

if ([mediaFile isKindOfClass:[UIImage class]])
        {
          [formData appendPartWithFileData:imageData name:@"media" fileName:@"picture.png" mimeType:@"image/png"];  
        }
    else if(isThisAudio)
    {
       [formData appendPartWithFileData:mediaFile name:@"media" fileName:@"audio.caf" mimeType:@"audio/caf"];  
    }
    else
    {
        [formData appendPartWithFileData:mediaFile name:@"media" fileName:@"movie.mov" mimeType:@"video/quicktime"];  
    }

}];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request

success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
    NSDictionary *jsonDictionary = JSON;       
    NSLog(@"jsonDictionary: %@",jsonDictionary);
}
failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
{
    NSLog(@"Failed: %@",[error localizedDescription]);
}];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

    percentDone = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite)); 
    NSLog(@"Uploaded %f",percentDone);
}];
[httpClient enqueueHTTPRequestOperation:operation];
NSMutableDictionary*dict=[NSMutableDictionary];
[dict setObject:twitid forKey:@“twit”];
[dict setObject:hash-forKey:@“hash”];
AFHTTPClient*httpClient=[[AFHTTPClient alloc]initWithBaseURL:url];
if([mediaFile IsKindof类:[UIImage类]])
{
imageData=UIImageJPEG表示(mediaFile,0.5);
}
NSMutableURLRequest*request=[httpClient multipartFormRequestWithMethod:@“POST”路径:nil参数:dict constructingBodyWithBlock:^(id formData){
if([mediaFile IsKindof类:[UIImage类]])
{
[formData appendPartWithFileData:imageData名称:@“媒体”文件名:@“picture.png”mimeType:@“image/png”];
}
else if(isThisAudio)
{
[formData appendPartWithFileData:mediaFile名称:@“media”文件名:@“audio.caf”mimeType:@“audio/caf”];
}
其他的
{
[formData appendPartWithFileData:mediaFile名称:@“media”文件名:@“movie.mov”mimeType:@“video/quicktime”];
}
}];
AFJSONRequestOperation*operation=[AFJSONRequestOperation JSONRequestOperationWithRequest:request
成功:^(NSURLRequest*请求,NSHTTPURLResponse*响应,id JSON)
{
NSDictionary*jsonDictionary=JSON;
NSLog(@“jsonDictionary:%@”,jsonDictionary);
}
失败:^(NSURLRequest*请求,NSURLResponse*响应,NSError*错误,id JSON)
{
NSLog(@“失败:%@,[error localizedDescription]);
}];
[操作设置LoadProgressBlock:^(NSUTEGER字节写入、long long TotalBytesWrite、long long totalBytesExpectedToWrite){
完成百分比=((浮点)((int)TotalBytesWrite)/(浮点)((int)totalBytesExpectedToWrite));
NSLog(@“已上载%f”,完成百分比);
}];
[httpClient排队HttpRequestOperation:operation];

一个很好的选择是使用类似的工具来监视通过网络实际发送的内容。通过这种方式,您可以看到网络上实际发生的情况,而不是代码流中某个点的状态。

签出类别

使用该类别,您可以简单地:

NSLog(@"%s %@", __FUNCTION__, [request cURLCommand])

我看不到任何代码行,你在哪里向请求添加任何JSON数据我正在发送一个文件和一个名为dict的字典。我想在发送之前打印出所有内容。也许我是盲的,但我找不到添加“JSON dict”的代码行,请注意,你没有向服务器发送JSON。您发送包含3个文件的多部分表单请求。可能是您收到了JSON。我能不能在控制台中打印出来?不能使用Charles代理方法。要在控制台中打印,您需要在代码中生成数据的某个点添加NSLog。