Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use Restkit to POST JSON and map response
    primarykey
    data
    text
    <p>I'm using RestKit for the first time, and its feature-set looks great. I've read the document multiple times now and I'm struggling to find a way to POST JSON params to a feed and map the JSON response. From searching on stackoverflow I found a way to send the JSON params via a GET, but my server only takes POST.</p> <p>Here is the code I have so far:</p> <pre><code>RKObjectMapping *issueMapping = [RKObjectMapping mappingForClass:[CDIssue class]]; [objectMapping mapKeyPath:@"issue_id" toAttribute:@"issueId"]; [objectMapping mapKeyPath:@"title" toAttribute:@"issueTitle"]; [objectMapping mapKeyPath:@"description" toAttribute:@"issueDescription"]; RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://restkit.org"]; RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"News.sqlite"]; objectManager.objectStore = objectStore; NSDictionary params = [NSDictionary dictionaryWithObjectsAndKeys: @"myUsername", @"username", @"myPassword", @"password", nil]; NSURL *someURL = [objectManager.client URLForResourcePath:@"/feed/getIssues.json" queryParams:params]; [manager loadObjectsAtResourcePath:[someURL absoluteString] objectMapping:objectMapping delegate:self] </code></pre> <p>From the another stackoverflow thread (http://stackoverflow.com/questions/9102262/do-a-simple-json-post-using-restkit), I know how to do a simple POST request with the following code:</p> <pre><code>RKClient *myClient = [RKClient sharedClient]; NSMutableDictionary *rpcData = [[NSMutableDictionary alloc] init ]; NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; //User and password params [params setObject:password forKey:@"password"]; [params setObject:username forKey:@"email"]; //The server ask me for this format, so I set it here: [rpcData setObject:@"2.0" forKey:@"jsonrpc"]; [rpcData setObject:@"authenticate" forKey:@"method"]; [rpcData setObject:@"" forKey:@"id"]; [rpcData setObject:params forKey:@"params"]; //Parsing rpcData to JSON! id&lt;RKParser&gt; parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:RKMIMETypeJSON]; NSError *error = nil; NSString *json = [parser stringFromObject:rpcData error:&amp;error]; //If no error we send the post, voila! if (!error){ [[myClient post:@"/" params:[RKRequestSerialization serializationWithData:[json dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON] delegate:self] send]; } </code></pre> <p>I was hoping someone would help me marry these two code snippets into a workable solution.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload