How to get JSON parsed object from RKObjectManager response block - restkit-0.20

I'm sending a getObjectsAtPath request like below. Everything works fine but i'm trying to find a way the have the parsed json response available in the success block. As far as i know only response as string or data is available from the operation.HTTPRequestOperation or an array of mapped objects from the mappingResult.array.
Since i need post treatment based on values specific attributes in the JSON response i'm loins to get the parsed JSON response.
Any idea ? If this is not possible as is, how to parse the responseString using rest kit of AFNetwork parsing mechanism ?
Thanks
[self.objectManager getObjectsAtPath:#"api"
parameters:#{#"a11call": #"wallactivityById"}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

Related

Afnetworking 2.0 PUT image

I am able to use put method of afnetworking 2.0 successfully for putting data.
NSString* PUTURL = [NSString
stringWithFormat:#"%#/updateestado/estado/%#/idJugador/%ld",BASEURl,[status
urlEncodeUsingEncoding:NSUTF8StringEncoding],userId];
NSLog(#"REG URL----%#",PUTURL);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager PUT:PUTURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
[self.NET_Delegate DelegateUpdateStatusResponce:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self.NET_Delegate DelegateUpdateStatusError:error];
}];
I am able to upload video successfully using post method.
But The requirement for uploading image is using PUT
I followed the way given in
Simple "PUT" file upload with AFNetworking
but had two issues says multipartFormRequestWithMethod deprived &
when trying the approach get 404 error.
No reference for this on afnetworking doc in github.
Query: I am working on uploading image using put first time, so i think i am missing some thing. Any reference or code samples to achieve this will be helpful. Thanks
You are not uploading an image; but rather a URL of the image (note the url parameter).
Therefore you will need to upload the image to a 3rd party site and then post the link to whatever that service is.
It's impossible to upload an image using a PUT request so you must be missing something.

What happens if AFHTTPSessionManager's responseSerializer fails to parse?

If AFHTTPSessionManager's responseSerializer fails to parse the response, eg the response isn't a valid JSON payload, what happens with the following code? Does:
A: The success block get called with responseObject=nil, or:
B: The failure block get called?
[[ServiceManager sharedManager].sessionManager GET:#"blah" parameters:params success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) {
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
Per the documentation, it's B; the failure block is executed when an error is encountered while parsing the response data.
I just checked and for me, the localizedDescription on the error was "The data couldn’t be read because it isn’t in the correct format."

Error with RestKit putObject with 204 response Xcode

Im developing a iOS RESTFul client App but I´m in trouble with the 204 response for a PUT request, this is my code:
[objectManager putObject:dataObject path:path
parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"Register Success");
completionBlock(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"Register Failed %#", error);
completionBlock(NO);
}];
Everything works, but I´m getting the following error:
E restkit.network:RKResponseMapperOperation.m:320 Failed to parse response data: Loaded an unprocessable response (204) with content type 'text/plain'
NSUnderlyingError=0x16ec4b60 "Cannot deserialize data: No serialization registered for MIME Type 'text/plain'", NSLocalizedDescription=Loaded an unprocessable response (204) with content type 'text/plain'}
response.body=No Content
I´m using pod 'RestKit', '~> 0.23.0'
I hope somebody knows how can I solve that
Thanks in advance!!!
The simplest option is to tell RestKit to treat the text mime type as JSON:
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:#"text/plain"];
Alternatively, use RKMapperOperation to serialise your object to be sent (or use the object manager to create the request to send) and then pass it on to the HTTP client (or enqueue an operation with the object manager). Check the RestKit github page for examples.

AFNetwork 2.0 POST results in a 403 error

I'm using AFNetworking 2 to perform a simple post operation:
[self.manager POST:#"person"
parameters:(NSDictionary *)parameters
constructingBodyWithBlock:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success)
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure)
failure(error);
}];
Every time this runs, XCode's console says that I got "Request failed: forbidden (403)" as the response. If I run against the exact same url shown in the NSErrorFailingURLKey via curl, I immediately get back the results I'd expect from the POST operation.
I haven't enabled any type of authentication on the script being called. It's just a Restler class. Am I missing a step here?
Adding the 'constructingBodyWithBlock' changed the type of message being sent. Had to remove that in order to make it work.

Objective-C: __block not working

I could not figure out how to change the value of results inside the success block. I use __block like some post suggests but results is forever nil. I set breakpoint inside of block and make sure that JSON is not nil, which download data as I expected.
I am using AFNetworking library if that's relevant.
+(NSArray *)eventsByCityID:(NSString *)cityID startIndex:(NSUInteger)start count:(NSUInteger)count
{
__block NSArray *results = nil;
[[DoubanHTTPClient sharedClient] getPath:#"event/list" parameters:#{#"loc":dataSingleton.cityID} success:^(AFHTTPRequestOperation *operation, id JSON) {
results = [JSON valueForKey:#"events"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"download events error: %# \n\n",error);
}];
return results;
}
More likely than not, that [very poorly named] method getPath:parameters:success:failure: is asynchronous.
Thus, you need to tell something in the success block that the value has changed. I.e.
^{
[something yoManGotEvents:[JSON valueForKey:#"events"]];
}
(Methods shouldn't be prefixed with get outside of very special circumstances. Third party libraries with lots of API using that prefix outside of said circumstances raise question as to what other system specific patterns they may not be following.)