I am using RestKit 2.0 to send a core data entity and an image to a server with the 'multipartFormRequestWithObject' method. However, when the entity data arrives it is not in json format. If I send the entity using 'postObject' without an image then the data is in json format. I use the same RKObjectMapping for both situations.
What do I have to do to make the Object serialize to json? I tried
[request setValue:#"application/json" forHTTPHeaderField:#"content-type"];
But that didn't help and I already have my object manager settings as so:
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
My Header Content-Type is multipart/form-data but I guess that is required.
request.headers={
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Accept-Version" = 1;
"Content-Length" = 19014;
"Content-Type" = "multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY";
"User-Agent" = "app/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)";
}
My complete code is for the mapping and operation are below. As usual any feedback would be great. Thanks. Al
- (void)loginMainUser:(NSDictionary*)paramsDict path:(NSString *)apiPath{
RKObjectManager *manager = [RKObjectManager sharedManager];
// Response Mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
[mapping addAttributeMappingsFromDictionary:#{#"token" : #"token",
#"_links" : #"links"}];
[manager addResponseDescriptorsFromArray:#[[RKResponseDescriptor responseDescriptorWithMapping:mapping
method:RKRequestMethodAny
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]];
// Request Mapping
RKObjectMapping *userRequestMapping = [RKObjectMapping requestMapping];
[userRequestMapping addAttributeMappingsFromDictionary:#{#"name" : #"first_name",
#"surname" : #"last_name",
#"email" : #"email",
#"password" : #"password"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMapping
objectClass:[self class]
rootKeyPath:nil
method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];
UIImage *image = [UIImage imageNamed:#"logo.png"];
// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [manager multipartFormRequestWithObject:self
method:RKRequestMethodPOST
path:apiPath
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(image)
name:#"logo"
fileName:#"logo.png"
mimeType:#"image/png"];
}];
RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"Success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"Failed");
}];
[manager enqueueObjectRequestOperation:operation];
}
multipartFormRequestWithObject is explicitly not JSON. This is by design. It's the HTTP content type that is changed so JSON and multipart form are mutually exclusive.
So, you need to change your mind about what you're trying to achieve.
One option could be to use a mapping operation to create the JSON for your object and then supply that JSON when you call multipartFormRequestWithObject. This would give you a multipart form message being sent with a section of JSON that could be deserialised on the server.
It is not the best approach but if you really need to get the JSON from a object with RestKit, you can convert it using this code:
// The object you want to convert
YourObject *object = ...;
// The RestKit descriptor that you want to use to map.
RKRequestDescriptor *requestDescriptorObject = ...;
NSDictionary *parametersForObject;
parametersForObject = [RKObjectParameterization parametersWithObject:object
requestDescriptor:requestDescriptorObject
error:nil];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parametersForObject
options:NSJSONWritingPrettyPrinted
error:nil];
NSString *jsonString;
if(jsonData) {
jsonString = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
}
And then with the JSON string you can post it in a multi-part post. It can be done with any method, but as you have already RestKit, you can do it with the library.
For example, at the code of below there is a post of a file and the JSON string (in fact, the JSON data).
// The RestKit manager
RKObjectManager *manager = ...;
// The server endpoint
NSString *path = ...;
NSMutableURLRequest *request = [manager multipartFormRequestWithObject:nil method:RKRequestMethodPOST path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// Post of the file
[formData appendPartWithFileData:[NSData dataWithData:self.dataFile]
name:#"file"
fileName:filename
mimeType:mimetype];
// Post of the JSON data
[formData appendPartWithFormData:jsonData name:#"json_data"];
}];
Related
is it possible to pass custom data in al_applink_data using Facebook applinks?
I can retrieve this JSON example but I cannot see a place where to append my custom data to it. If this is not possible than my only solution is to parse obtained URL but this doesn't seem much bulletproof.
{
"target_url": "https://www.example.com/abc.html",
"extras": {
"fb_app_id": [YOUR_FACEBOOK_APP_ID],
"fb_access_token": "[ACCESS_TOKEN']",
"fb_expires_in": "3600"
},
"referer_app_link": {
"url": "[FACEBOOK_APP_BACK_LINK]",
"app_name": "Facebook"
}
}
Parsing Data
My solution by creating custom data for target_url.
NSDictionary *dictionary = #{ #"target_url" : #"YOUR_VALUE"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Then, append with your Facebook app link ID with al_applink_data key in FB Graph Object dictionary.
[NSString stringWithFormat:#"https://fb.me/FB_LINK_ID?al_applink_data=%#", jsonString]
That's it.!!
Retrieving Callback URL
if([[call appLinkData] targetURL] != nil)
{
NSURL *targetUrl = [[call appLinkData] targetURL];
//Actual URL
NSString *urlString = [[targetUrl absoluteString] stringByRemovingPercentEncoding];
URLParser *parser = [[URLParser alloc] initWithURLString:urlString];
//Fetching value for 'al_applink_data'
NSString *appLinkData = [parser valueForVariable:#"al_applink_data"];
NSData *objectData = [appLinkData dataUsingEncoding:NSUTF8StringEncoding];
//Dictionary with 'target_key' key and its value.
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"%#", json);
}
Reference for URL parsing : URLParser
Thanks.
The "extras" map was designed to carry arbitrary metadata. What type of custom data do you need? Passing of custom data through the "extras" blob requires the caller to know something about your app (so they can actually add the data).
In reading the documentation and sample code for posting objects, I must have missed something relating to serializing entities. It appears possible to send an entity to postObject and expect it to use the supplied mapping to produce JSON and POST it to a server.
I have been able to map and post an object, but the JSON is not coming through well formed.
I have been able to hand parameterize the object and get valid JSON. I'm about to dig into the source-code, but was wondering what I'm missing.
Here's the code and the results I'm seeing. Insight/help is appreciated!
I have the following Managed Object:
#interface TFUser : NSManagedObject
#property (nonatomic, retain) NSString * first_name;
#property (nonatomic, retain) NSString * last_name;
#end
I have the following code to map it:
+(RKEntityMapping *) mapping
{
if (_mapping == nil)
{
RKObjectManager *objectManager = [RKObjectManager sharedManager];
assert(objectManager && "Object manager not initialized!?");
RKManagedObjectStore *managedObjectStore = objectManager.managedObjectStore;
assert(objectManager && "No object store!?");
// USER Entity Map
_mapping = [RKEntityMapping mappingForEntityForName:#"TFUser"
inManagedObjectStore:managedObjectStore];
[_mapping addAttributeMappingsFromDictionary:#{
#"first_name": #"first_name",
#"last_name": #"last_name"
}];
_mapping.identificationAttributes = #[ #"first_name" ];
}
return _mapping;
}
I have the following code to test serializing one Managed Object directly:
TFUser *user = (TFUser*) [self.managedObjectContext insertNewObjectForEntityForName:#"TFUser"];
user.first_name = #"Mickey";
user.last_name = #"Mouse";
NSError *error;
RKRequestDescriptor *requestDescriptor =
[RKRequestDescriptor requestDescriptorWithMapping:[TFUser.mapping inverseMapping]
objectClass:[TFUser class]
rootKeyPath:#"user"
method:RKRequestMethodAny];
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:user
requestDescriptor:requestDescriptor
error:&error];
NSData *jsonData= [RKMIMETypeSerialization dataFromObject:parameters MIMEType:RKMIMETypeJSON error:&error];
NSString *message = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];
NSLog(#"USER JSON:\r\n%#", message);
And this produces what I'd expect:
USER JSON:
{
"user" : {
"first_name" : "Mickey",
"last_name" : "Mouse"
}
}
When I postObject the Managed Object directly:
[[RKObjectManager sharedManager] postObject:user
path:#"/user"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"Success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"Error");
}];
The server receives:
{ user: { first_name: 'Mickey', last_name: 'Mouse' } }
And the parser complains that it can't process the JSON.
Why am I getting a different result? How can I get the post to produce the correct JSON for the server?
Thanks!
- Kevin
The code has been working the entire time. The debugging and some code on the server was broken.
By carefully inspecting the logging of the body request being sent to the server it was apparent that the JSON was well-formed leaving the client. Here it is:
request.headers={
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Treefort/1.0 (iPhone Simulator; iOS 7.0.3; Scale/2.00)";
}
request.body={
"user" : {
"first_name" : "Mickey",
"last_name" : "Mouse"
}
}
The code on the server was doing this:
console.log req.body
request = JSON.parse req.body
user = request.user
The JSON has already been processed into objects by the time it gets here. This means that the log line is dumping the object to the log, not the text of the received body. The second problem is that there is no reason to be explicitly requesting a JSON parse of the body. It's already been converted to objects.
To fix it I just changed the code to be:
user = req.body.user
Sometimes it helps to have somebody else say... it should be working. I've been banging my head on this for days. Thanks!
I have an API that requires I post a complex JSON object. The API saves and responds with a primitive ID (or an error object). I am not able to map the primitive ID. Any ideas? The example below, for simplicity's sake, is not doing the POST object mapping, but some of my API calls will require that as well.
I saw suggestions to utilize RestKit to build the request, and pass it to AFNetworking, but this will not parse a possible error return object.
RKObjectMapping* map = [RKObjectMapping mappingForClass:[MyPrimitiveResponse class]];
[map addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:#"success"]];
RKResponseDescriptor *errDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MyErrorResponse objectMap] method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError)];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:map method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:apiUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:#[ errDescriptor, responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
RKLogInfo(#"Load collection of Articles: %#", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(#"Operation failed with error: %#", error);
}];
[objectRequestOperation start];
I get the following error in the debugger:
2013-10-29 10:23:15.196 TestParser[6988:70b] E app:MyHomeViewController.m:54 Operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'" UserInfo=0x8daca20 {NSErrorFailingURLKey=....., NSUnderlyingError=0x8db4cd0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}
UPDATE:
This is my final bits of code to handle this situation. It may prove useful to others...
// Manually Map Request to JSON & send to server
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:payload requestDescriptor:[payload.class requestDescriptor] error:&error];
NSMutableURLRequest* request = [self.apiManager requestWithObject:nil method:RKRequestMethodPOST path:url parameters:parameters];
RKHTTPRequestOperation *requestOperation = [[RKHTTPRequestOperation alloc] initWithRequest:request];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
unichar firstChar = [operation.responseString characterAtIndex:0];
NSData *newData;
if (firstChar != '{' && firstChar != '[') {
// Force into JSON array so it can be parsed normally
newData = [[[#"[" stringByAppendingString:operation.responseString] stringByAppendingString:#"]"] dataUsingEncoding:NSUTF8StringEncoding];
} else {
newData = operation.responseData;
}
// Parse JSON response into native object, whether primitive NSNumber (integer or boolean) response or a full fledged JSON error object.
RKResponseDescriptor *errDescriptor = [MyErrorResponse responseDescriptor];
RKObjectResponseMapperOperation* mapper = [[RKObjectResponseMapperOperation alloc] initWithRequest:request response:operation.response data:newData responseDescriptors:#[errDescriptor, [payload.class responseDescriptor]]];
[mapper setDidFinishMappingBlock:^(RKMappingResult *mappingResult, NSError *error) {
if (mappingResult) { //Success
RKLogInfo(#"Load response: %#", mappingResult.firstObject);
} else {
RKLogError(#"Operation failed with error: %#", error);
}
}];
[mapper start];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
RKLogError(#"Operation failed with error: %#", error);
}];
[requestOperation start];
I saw suggestions to utilize RestKit to build the request, and pass it to AFNetworking
Yes, do that.
but this will not parse a possible error return object
True, but you can use a mapping operation in RestKit to process that. Or, if the error response is relatively simple just use NSJSONSerialization (or similar) to convert the response.
The 'primitive ID' should be mappable with a nil-keypath mapping. But you may still have issues in mapping an error response if it isn't being mapped back into the source object used in the POST request.
I have a problem to download a file or manage an error in some cases (JSON)
The download works very fine. The problem is the server can send a 404 or 200 (with JSON) in a few cases when the user requests the download.
How to handle the JSON in this case? When we send the request we don't know if we will receive JSON error (with a 200 status oR 404) or the zipped file...
I don't see how responseObject can help me.
Here is my code:
AFHTTPRequestOperation *operation = [[[AFHTTPRequestOperation alloc] initWithRequest:request] autorelease];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:zipPath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"Successfully downloaded zip file to %#", zipPath);
// is-it impossible to handle a JSON response here ?
// responseObject can help me ? don't think !
// do what I have to do after the download is complete
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
// 404
// is-it impossible to handle a JSON response here ?
if ([error code] == -1011)
{
}
}];
[operation setDownloadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
float progress = ((float)((int)totalBytesWritten) / (float)((int)totalBytesExpectedToWrite));
self.progressView.progress = progress;
}];
[operation start];
Does I need to make a AFJSOnRequestOperation ? But in this case, how to receive the downloaded file that is not JSON ?
Thanks for helping.
EDIT: as I wroted in my comments, I can get and catch the responseData in success block ONLY if I comment the line:
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:zipPath append:NO];
It's logical, I suppose the response goes into outputStream instead of success block. Is there a solution for that ?
The server should signal the MIME type in the header Content-Type.
If it doesn't you need to detect the data type yourself.
This line ensures to get all responses with statuscode 200-499 in the success block:
[AFHTTPRequestOperation addAcceptableStatusCodes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(300, 200)]];
In the success block you need to identify the type of response:
id jsonObject = AFJSONDecode(operation.responseData, NULL);
if (jsonObject) {
// handle JSON
} else {
// handle zip file
}
You can also inspect operation.response.statusCode and operation.response.MIMEType beforehand.
I've tried with AFJSOnRequestOperation, in .m I simply put the outputStream beneath the init:
AFJSONRequestOperation *requestOperation = [[[self alloc] initWithRequest:urlRequest] autorelease];
requestOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:[FullyLoaded filePathForResourceAtURL:urlRequest.URL.absoluteString] append:NO];
And the same happens, responseData is nil unless commenting the outputStream line, so it's not HTTPRequest or JSOnRequest issue. The way I use to get around is to JSON sequence the object from the responseData and write it to file, rather than directly outputstream the request.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFCompoundResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"application/octet-stream"];
AFHTTPRequestOperation *operation = [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
}
else{
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[operation start];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"application/octet-stream"]; : can vary based on what you expect
This is my string:
[{"id":"1","nome":"Adriatik"},{"id":"2","nome":"Ard"},{"id":"3","nome":"Albana"},{"id":"4","nome":"Adriana"}]
I would like to parse all 'name' of the JSON string into a NSMutableArray.
Sorry for my english!
Whenever I have to handle some JSON code, the first thing I like to do is create a class based on the JSON text. So, for example if your JSON is representing a U.S. state, create a "State" class.
There's a cool little product that you can use for this. It's called Objectify and costs about $15. No doubt people can advise on other free stuff that might do something similar.
For the actual Json parsing, I use SBJson. There's quite a few Json parsing frameworks out there for Objective-C so definitely have a look around to see what takes your fancy.
Next, with SBJson, do the actual parsing:
-(NSDictionary *)parseJsonFromUrl
{
NSAssert(mUrl, #"Must set a url before invoking %#", __PRETTY_FUNCTION__);
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download JSON
NSURLRequest *request = [NSURLRequest requestWithURL:mUrl];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
return [parser objectWithString:json_string error:nil];
}
That returns a NSDictionary. You know have to look through that dictionary to set the values of your model class. Here's how to do that whilst at the same time loading the values into the NSMutableArray:
-(void)downloadJsonData
{
NSDictionary *statesDict = [self parseJsonFromUrl];
NSMutableArray *statesArray = [NSMutableArray arrayWithCapacity:[statesDict count]];
for (NSDictionary *stateDict in stateDict)
{
State *aState = [[[State alloc] init] autorelease];
aState.stateId = [stateDict valueForKey:#"id"];
aState.name = [stateDict valueForKey:#"name"];
[statesArray addObject:aState];
}
}
Note that I use a property name of stateId not id so as not to clash with the Objective-C object pointer type.
Use SBJson classes and call -JSONValue method
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// NSLog(#" Response String %#", responseString);
//converted response json string to a simple NSdictionary
NSMutableArray *results = [responseString JSONValue];