I'm having some trouble with RESTfull web service and I'm trying to see my request as a text using NSLog. I tried this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
...
[manager POST:urlString parameters:mutableParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Response: %#", [operation description]) ;
if (block) {
block(error);
}
NSLog(#"-------------------------------");
NSLog(#"Request: %#", manager.requestSerializer.debugDescription);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", manager.requestSerializer.description);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", operation.request.HTTPBodyStream);
NSLog(#"-------------------------------");
NSLog(#"Request: %#", operation.request);
NSLog(#"-------------------------------");
NSLog(#"Error: %#", error);
NSLog(#"-------------------------------");
}];
Is there any way to NSLog the request from AFHTTPRequestOperationManager (AFNetworking 2) ?
Have a look to the POST method:
[manager POST:urlString parameters:mutableParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
You have a AFHTTPRequestOperation *operation and an id responseObject in the success block.
The best thing that you can do is to put a:
NSLog(#"AFHttpRequestOperation %#", operation);
and set there a breakpoint so you can inspect what's happening there:
What do you want to see about the operation's request?
Not exactly logging but this answer explains how to convert NSURLRequest to NSString
Related
in my app I'm using the new AFN 3.0 and I have
AFHTTPSessionManager *manager
instead of
AFHTTPRequestOperation *operation
my problem is that before I was able to get some data from RequestOperation as:
NSURL *url = operation.request.URL;
//or
NSNumber statusCode = operation.response.statusCode;
//or
NSData *responseData = operation.responseData;
and how can I get this elements with AFHTTPSessionManager?
thanks
in v2 you were getting AFHTTPRequestOperation for the request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
But in the v3 you will get NSURLSessionTask
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
So based on that you can get the details the from the NSURLSessionTask like the currentRequest , response etc
For more changes and details, you can refer to the migration guide of AFNetworking
AFNetworking Migration Guide
For NSURLSessionTask Reference : NSURLSessionTask
I am doing the migration of AFNetworking library from 1.x to 3.x for my project.
As per my understanding the AFHTTPRequestOperation to be replaced with AFHTTPSessionManager.
What is the replacement for the method cancel and property isCancelled,isReady, request and response that are present in the AFHTTPRequestOperation class.
Help appreciated.
In AFHTTPRequestOperationManager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
In AFHTTPRequestOperation
NSURL *URL = [NSURL URLWithString:#"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am trying to have an API Call post. I can use this post command however my issue is that I have multiple parameters such as, Name, phone number, email, and multiple other private information. I have tried this method in order to get it to work but have not been successful. Thank you in advance for any help!
GET Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
or
POST URL-Form-Encoded Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar"};
[manager POST:#"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar", #"key_param2":#"obj_param2", #"key_param3":#"obj_param3",#"key_paramN":#"obj_paramN"};
[manager POST:#"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
To add more parameters, just include them in your parameters dictionary like so:
NSDictionary *parameters = #{#"foo": #"bar", #"otherFoo":#"otherBar"};
Then pass 'parameters' to your post method exactly as you are currently doing.
I've already killed a couple terrible hours to find out how to save a responseObject in a Get request AFNetworking to nsarray. Can't figure out how to save it so I could use it in another part of my code.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
__block NSArray* responseObjectArray = nil;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
responseObjectArray = (NSArray*)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
this is always error with -1004 ( could not connect to server )
but the request in browser is working fine.
what's wrong with my code?
(url : "http://localhost:3000/v1/voice/version?appname=wevoice")
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:#"http://localhost:3000/v1/voice/version"
parameters:#{ #"appname": #"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
You have to set the property baseURL on your request manager. Here is an example:
self.requestManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:http://localhost:3000];
Later, in your concrete GET message to the manager, you'll only pass the relative URL path:
[self.requestManager GET:#"/v1/voice/version"
parameters:#{ #"appname": #"wevoice" }
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
And keep in mind, that, if you want to send your paramters as JSON, you'll need a request serializer:
self.requestManager.requestSerializer = [AFJSONRequestSerializer serializer];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:#"wevoice" forKey:#"appname"];
[manager GET:#"http://localhost:3000/v1/voice/version"
parameters:dic
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
You can try this.May be the way you add Parameter is wrong.
Try this method. Hope it helps.
+ (void)getRequestWithURLStringWithSuccess:(void(^)(id object))successBlock andFail:(void(^)(NSString *errorMessage))failBlock {
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:#"http://localhost:3000/v1/voice/version"]];
NSString *urlString = #"/version";
[manager GET:urlString parameters:#{ #"appname": #"wevoice" } success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}