RestKit 0.20.0 - getObjectsAtPath with JSON HTTP Body request - objective-c

In RestKit Version 0.10.x I used
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/special/path" usingBlock:^(RKObjectLoader *loader) {
loader.objectMapping = aMapping;
loader.delegate = _delegate;
loader.method = RKRequestMethodPOST;
loader.userData = [MobileGatewayReauthentication class];
loader.params = [anDictionary toJsonRKParam];
}];
to add a NSDictionary to loader.params to send a collection of parameters as the HTTP body of the request.
Since RestKit 0.20.0 the method loadObjectsAtResourcePath has been replaced by the following method, where it is no longer possible to pass a NSDictionary in parameters, which is used as HTTP Body (JSON encoded):
[RKObjectManager.sharedManager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(#"success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"failure");
}];
When working with [[RKObjectManager sharedManager].HTTPClient (AFNetworking) it works like a charm when setting:
[[RKObjectManager sharedManager].HTTPClient setParameterEncoding:AFJSONParameterEncoding];
[[RKObjectManager sharedManager].HTTPClient postPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#",error.localizedDescription);
}];
But I need the higher-level object mapper.
[[RKObjectManager sharedManager].HTTPClient setParameterEncoding:AFJSONParameterEncoding];
has no effect on [RKObjectManager sharedManager] but
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"aHeader" value:#"aValue"];
has. So I assumed that setParameterEncoding works, too.
Is it a bug, is it not yet implemented, or have I missed something?
Thanks a lot.

I really have missed something.
loadObjectsAtResourcePath get's (for sure) called with RKRequestMethodGET. So the parameters get append to the URL, not to the HTTP body.
To append the parametrs as a JSON HTTP body, you have to use a post method, for example
- (void)postObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure;
Excuse the hasty question. I should have seen it before.

If I understood your problem correctly, RestKit creator pushed something in the development branch (which is always tested and pretty safe to use) that might fix your problem (Commit here), which basically sets by default this:
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"Accept" value:RKMIMETypeJSON];
[[RKObjectManager sharedManager].HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
Hope this help!

There could be problem with the path in getObjectsAtPath:path
If you are using slash (/) in the beginning of path, move it end of baseurl as shown in example below.
NSURL *baseURL = [NSURL URLWithString:#"http://rest.example.com:8080/test/"];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[objectManager getObjectsAtPath:#"rest/updates"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"failure");
}];

Related

Forming Multi-part request through RestKit

This is my code to post multi-part data to server. What i observe is there is only image data being included in request, not the article object. Please let me know if someone has done this stuff successfully in the past. Thanks in advance.
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:#[#"title", #"author", #"body"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Article class] rootKeyPath:#"article" method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];
Article *article = [Article new];
article.title = #"Introduction to RestKit";
article.body = #"This is some text.";
article.author = #"Blake";
NSMutableURLRequest *request = [objectManager multipartFormRequestWithObject:article
method:RKRequestMethodPOST
path:path
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:imageInfo
fileName:imageName
mimeType:#"image/jpeg"];
}];
RKObjectRequestOperation *operation1 = [objectManager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
// Process data
NSLog(#"success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// An error occurred
NSLog(#"error");
}];
Resolved this using appendPartWithHeaders. Source.
Lemme know if someone needs to see implementation more closely.

Afnetworking wrong block is returned

I am using afnetworking and AFHTTPRequestOperationManager,
I have a singleton class, which contains all my api call. However, when I have concurrent api call, wrong data is being returned. API call A is returning API call B response?
CHAFHTTPRequestOperationManager is a subclass of AFHTTPRequestOperationManager
Anyone experience the same problem, what do I need to do to solve this:
NSString *path = [NSString stringWithFormat:#"users/%#/profile_photo", userName];
CHAFHTTPRequestOperationManager *manager = [CHAFHTTPRequestOperationManager sharedManagerObj];
[manager GET:path
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
];
}

Get plain response from AFNetworking 2.4.1

I'm sending a POST request:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSDictionary *parameters = #{#"method": #"login", #"email":#"email", #"password":#"password"};
[manager POST:#"http://www.apiwebsite.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
My responseObject is:
JSON: <0d0a7b22 72657370 6f6e7365 223a224f 4b222c22 30223a22 6d61696c
696e675f 6b657922 2c223122 3a226239 65653766 34643238 64333730
30666663 37393734 65376665 66626663 3136227d>
But calling the api with POSTMAN i get:
{"response":"OK", "id":"1234223123"}
How can I get the plain or JSON response to work with it?
thanks!
To get JSON object use proper serializer AFJSONResponseSerializer instead of AFHTTPResponseSerializer you uses now.
For debug purpose convert NSData to NSString
NSLog(#"JSON: %#", [[NSString alloc] initWithData: responseObject encoding:NSUTF8StringEncoding]);

What is the simplest way to use multipart/form-data in AFNetworking 2.0? ios7

I need to send all my forms on server, firstly text forms and then images. Do someone working with it?
The documentation page of AFNetworking shows the following example:
Multi-Part Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar"};
NSURL *filePath = [NSURL fileURLWithPath:#"file://path/to/image.png"];
[manager POST:#"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:#"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

Can't get HTML code AFNetworking 2.0

I tried to make GET HTTP response. I need to get the html code for the subsequent parsing, but responseObject is nil.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager GET:#"http://www.example.com/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
HTMLParser *parser = [[HTMLParser alloc] initWithString:responseObject error:&error];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
For get html code we will need to build a custom response serializer to decode the NSData response from the web server into a NSString. We will need to subclass AFHTTPResponseSerializer and implement the following method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
Why for example you have not use this solution below instead of subcalssing. It does the same thing, but you don't need to create additional files, just for overload one method.
So you can just add encoding your responseObjet in the block for example, and it will work as well. I am using POST in my example but it should work with GET in the same way but without parameters, but idea of the just conversation.
+ (void)makeRequestWithParams:(NSDictionary *)params
success:(OperationCompletionBlock)success
failure:(OperationCompletionBlock)failure
{
NSString *path = #"http://www.example.com/";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFCompoundResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString* encodedString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#", encodedString);
success(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
failure(nil);
}];
}