Error with RestKit putObject with 204 response Xcode - objective-c

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.

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.

How to get JSON parsed object from RKObjectManager response block

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) {
}];

iOS special characters in url

I try to make an api call:
api.app.com/foo/search/Öhm
where Öhm is the search term. The problem is that this url causes a bad url exception, while normal chars work well. I tried with
[searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
However this produces a lot of % signs. The api expects Öhm. Any solution for this problem? I am using restkit.
The full error:
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x16e719d0
{NSUnderlyingError=0x16d70d10 "bad URL", NSLocalizedDescription=bad
URL} 2014-04-03 18:26:55.404 My App[6844:3807] E
restkit.network:RKObjectRequestOperation.m:243 GET '(null)' (0 / 0
objects) [request=0.0084s mapping=0.0000s total=0.0769s]: Error
Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x16e719d0
{NSUnderlyingError=0x16d70d10 "bad URL", NSLocalizedDescription=bad
URL}
The termin is (null) instead of Öhm.
Code:
[[ApiSearchManager sharedManager] search:^(NSArray *result){
//handle success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
//handle fail - it fails for Öhm
} query:searchTerm];
and on the lower level:
- (void) search:(void (^)(NSArray *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure query:(NSString *)query{
[self getObjectsAtPath:[NSString stringWithFormat:#"foo/search/%#", query] parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
if (success) {
success(mappingResult.array);
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if (failure) {
failure(operation, error);
}
}];
}
The % signs are escaping, and that is how the URL should be transmitted. At the server side, the URL gets interpreted (unescaped) and the server-side code will see the special characters as you intended. For example it's very common to see URL's with %20 in them. ASCII character 20 (hex) is a space, so server-side code that obtains the URL (appropriately decoded) see a space in there.
This section of the log:
GET '(null)' (0 / 0 objects) [request=0.0084s mapping=0.0000s total=0.0769s]:
Means you're doing a GET, that the request URL is nil (which would be bad), so status code was obtained because no request was actually sent, it took a little time, blah blah
So, The URL loading system couldn't create a valid URL with which to make your request.
I'm going to guess that the only way you can achieve this is to not have a properly configured baseURL in your RKObjectManager. Check that. (though I would expect that to fail earlier - so you may be reduced to debugging around RKObjectRequestOperation.m, line 243, and back up the call stack)

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.

"The network connection was lost" when loading a specific Resource

since a few days I have problems with RestKit 0.20
RestKit is included via CocoaPods so it is up to date. I am developing an app for iOS7 and this problems happen on all real and simulated devices.
When I am loading a resource like an image or a pdf file like this:
RKObjectManager *objectManager = [self.restManager objectManager];
[objectManager setAcceptHeaderWithMIMEType:#"image/jpeg"];
[[objectManager HTTPClient] getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (completeBlock) {
UIImage *image = [UIImage imageWithData:responseObject];
completeBlock(image);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failBlock) {
failBlock(error);
}
}];
I get this error message:
T restkit.network:RKObjectRequestOperation.m:148 GET 'https://***/attachment/11787':
request.headers={
Accept = "*";
"Accept-Language" = "en-US";
Authorization = "***";
"User-Agent" = "***";
}
request.body=(null)
E restkit.network:RKObjectRequestOperation.m:174 GET 'https://***/attachment/11787' (200 OK) [1.0048 s]:
error=Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0xf7ba400 {NSErrorFailingURLStringKey=https:/***/attachment/11787, NSErrorFailingURLKey=https://***/attachment/11787, NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x1605c930 "The network connection was lost."}
response.body=(null)
But this is not the common case for all resources but for only a few. The other resources get loaded at the same time (The requests were added to the queue at the same time).
Once this error happends for a resource, the resource will never get loaded again but failing with the same error message.
I cleaned the cache and deleted the app, so it shouldn't be a problem with caching or something like that. And there is no difference if http or https is used.
Similar to this question (without solution): Restkit not work on iOS 7
Do you have any ideas, what I am doing wrong or what causes this behaviour?