Order of Parameters in a POST in RestKit - ios

I have configured RestKit successfully so I can send out POST messages to the device I'm working with.
I have confirmed that when I use Chrome's Postman that the format of the message is correct.
I need to send......
id=87654321&content={"ts":1396215675,"payload":{"ssid_pass":"blah"}}&t=12345678
So in Chrome's postman, it goes out correct. But when I use RestKit postObject
[manager postObject:self path:#"/tom" parameters:#{#"id" : K_IDVALUE, #"content": myPayload, #"t":K_TVALUE, } success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"Successful Post!");
The order the device sees is wrong. It sees
content={"ts":1396215675,"payload":{"ssid_pass":"blah"}}&id=87654321&t=12345678
The device really wants the content to be between the id and t tags of the POST request. Is there a way to force RestKit to take the parameters as is? It appears that the message is being alphabetized (which I assume is happening at serialization).

Is there a way to force RestKit to take the parameters as is?
What you are passing into RestiKit:
#{#"id" : K_IDVALUE, … }
is a NSDictionary, which has no concept of ordering of its elements.
Your only chance then is that RestKit is using the mapping you specify to determine the order of the keys. I cannot check now into RestKit sources to confirm this hypothesis, but you could just try it out (or simply verify what is your mapping current ordering).

Related

Multiple objects are not getting saved cored data restkit

I am saving 4 different tokens on Server using Restkit Coredata.
using response descriptor.
RKResponseDescriptor *tokenDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:instagramToken method:RKRequestMethodGET pathPattern:#"register/token" keyPath:#"data" statusCodes:statusCodes];
// And I'm calling it this way.
[[RKObjectManager sharedManager] getObjectsAtPath:#"register/token" parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];
it is storing very well and I can access it in coredata as well. but the problem is,
If I save 1 token on server and map the response all good no issue in that. But when I save other token on Server the new response get mapped in coredata and over-right the old response.
I want to keep all the responses in coredata.
Please help on this.
The server needs to return a unique identity for each token so you know which is which. You need an attribute in your model to store that and you need to add that to the mapping and set it as the mapping identification attribute. Finally you need to connect your RK managed object store with an object cache so that it can search for duplicates to update (instead of always creating new instances).

How to handle the response for Feedly Authentication?

Feedly API Documentation
I'm trying to take advantage of Feedly APIs in my testing iOS app, but unfortunately it seems too great a confusion to me, a newbie to coding in general.
I encountered a problem at the very beginning.
The first step required is the Authentication, as shown in the link above. So I write the code below:
NSDictionary *queryParams = #{#"response_type" : #"code",
#"client_id" : clientID, //some NSString
#"redirect_uri" : redirectURL, //#"https%3A%2F%2Flocalhost/"
#"scope" : #"https://cloud.feedly.com/subscriptions"
};
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"https://developer.feedly.com/"]];
[httpClient getPath:#"/v3/auth"
parameters:queryParams
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"response: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"HTTP Requset Operation Error! %#", error);
}];
It appeared that I succeeded in getting the response when I ran the code, since the response was logged. However, the output was like this:
<3c21444f 43545950 45206874 6d6c3e0a 3c68746d 6c206c61 6e673d22 656e223e 0a3c6865 61643e0a 20203c6d 65746120 68747470 2d657175 69763d22 436f6e74 656e742d 54797065 2220636f 6e74656e 743d2274 6578742f 68746d6c 3b206368 61727365 743d5554 462d3822 202f3e0a 20203c6d 65746120 68747470 2d657175 69763d22 436f6e74 656e742d 4c616e67 75616765 2220636f 6e74656e 743d2265 6e2d7573 22202f3e 0a20203c 6d657461 20687474 702d657 ......(the list goes on and on)>
I'm really clueless about what type this is and how I can use it. Is it the response mentioned in the documentation?
Here is the part related to handling the response:
Handling the response
The response will be sent to the redirect_uri specified in the request. If the user approves the access request, then the response contains an code and the state parameter (if included in the request). If the user does not approve the request the response contains an error message. All responses are returned to the web server on the query string, as shown below:
An error response:
https://your.redirect.uri/feedlyCallback?error=access_denied&state=state.passed.in
A code response
https://your.redirect.uri/feedlyCallback?code=AQAA7rJ7InAiOjEsImEiOiJmZWVk…&state=state.passed.in
What does it mean "All responses are returned to the web server on the query string" and how can I access the returned parameters?(This seems to be the part where I'm stumped)
Thanks for anyone who can lead me out of this mess and give me faith to continue coding.
Plus I'm really fuzzy about how to cope with these web service APIs and the whole url-query/response thing. I'd be vastly grateful if someone can name a book or some article from which I can learn something regarding the way this mechanism works.
The response will be sent to the redirect_uri specified in the request.
Check the response url:
https://your.redirect.uri/feedlyCallback?error=access_denied&state=state.passed.in
Notice the feedlyCallback? part and query strings error and state
I don't know what language you are using, but for PHP: you should create feedlyCallback.php and set php server to feed files without .php extension,
in php files, you can acces query strings with $_GET['error'] and $_GET['state']
Just google for things you have problems with. No idea what query stings mean? http://en.wikipedia.org/wiki/Query_string

Restkit how to map arbitrary json (keys may change overtime) to NSDictionary

Let's say that in response to a PUT request:
[[RKObjectManager sharedManager]
putObject:nil
path:#"/api/users/add-user"
parameters:dictionary
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
NSLog(#"Success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error){
NSLog(#"Failure");
}
];
I get a "JSON" of unknown structure. I don't know what and how many keys to expect, but the values are all strings. How do I map it to a NSDictionary *object?
You can use a REST client to see the response and the structure of the JSON response, and create a corresponding model to map it against. Or if this is something that is not a consistent model, you can use NSJSONSerialization to parse the JSON into a KV pair, and your variable of interest in 'mappingResult'.
And you might want to look in AFNetworking 2.0. It works very well with your REST calls and parsing your JSON.
http://nshipster.com/afnetworking-2/
If the JSON response has an unknown structure then you either shouldn't be using RestKit (just use NSJSONSerialization and handle everything yourself) or you will need to use an RKDynamicMapping so that you can analyse the incoming data and decide what mapping to create (on-the-fly) and return to action the processing.

Retrieving HTTPBody from RestKit response

I am using the postObject and putObject functions of restKit. I need to extract some info from the payload and put it in the header before sending it out.. any suggestions on how I can do that?
ex:
[[RKObjectManager sharedManager] postObject:object path:OBJECT parameters:nil success:success failure:failure];
I will probably have to do something like
// Create Payload for this request
// update header accordingly
// call postObject
I would like to avoid throwing away existing code..
I was able to do this by using all the params that I was passing before making the RK request.

How to send a XML formatted POST request with RestKit 0.20.1

I have an iOS app that is using RestKit 0.20.1 to pull data from a server. The server at this time can send JSON formatted data but is not able to receive JSON formatted data.
This is where my problem is. POST requests require HTTP Body and the server is set up to receive XML. I have implemented the RKXMLReaderSerialization add on so I can receive XML but I can't find any current way of sending an XML formatted HTTP Body with RestKit.
This question "Send post request in XML format using RestKit " is what I was looking for but the answer from Imran Raheem is now (as far as I can tell) obsolete due to changes in RestKit.
I am using this method for the POST
[[RKObjectManager sharedManager] postObject:nil path:#"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];
Here is what the RestKit objectManager says about the postObject method
/**
Creates an `RKObjectRequestOperation` with a `POST` request for the given object, and enqueues it to the manager's operation queue.
#param object The object with which to construct the object request operation. If `nil`, then the path must be provided.
#param path The path to be appended to the HTTP client's base URL and used as the request URL. If nil, the request URL will be obtained by consulting the router for a route registered for the given object's class and the `RKRequestMethodPOST` method.
#param parameters The parameters to be reverse merged with the parameterization of the given object and set as the request body.
#param success A block object to be executed when the object request operation finishes successfully. This block has no return value and takes two arguments: the created object request operation and the `RKMappingResult` object created by object mapping the response data of request.
#param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the resonse data. This block has no return value and takes two arguments:, the created request operation and the `NSError` object describing the network or parsing error that occurred.
If I have the MIMEType set to JSON the Trace shows my request.body is being populated like so request.body={"Search":"Trending"}.
However if I set the MIMEType to XML the Trace shows the request.body=(null)
Here is the line I use to change the MIMEType
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
I am pretty new to iOS and Objective-C so I may be setting up the NSDictionary that is used in the parameters of the `postObject' method wrong. Here it is just in case....
NSArray *objects =[NSArray arrayWithObjects:#"trending", nil];
NSArray *keys =[NSArray arrayWithObjects: #"Search",nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
self.search=params;
Any help would be greatly appreciated! Given that I am new Snippets are especially helpful!
Oh and BTW if anyone can point me to REST method that accepts JSON input I would gladly pass it on to my server guy so I can just avoid XML all together.
As feared and suggested in other posts on this topic writing XML is not supported in version of RestKit 0.20.1. Here is a link to my conversation with Blake Watters on the subject.
https://github.com/RestKit/RestKit/issues/1430#issuecomment-19150316
I never did clear up why the request.body= (null) when the MIMEType is set to XML.
The manager of the server has agreed to set it up to receive JSON. That is how I plan to work around this.

Resources