posting JSON object to a Restful web service - ios

I have two Restful web services:
service1: api.wego.com/flights/api/k/2/searches and
service2: api.wego.com/flights/api/k/2/fares
service1's question:
I want to use AFJSONRequestOperation to do a POST request to serivce1 , the using of AFJSONRequestOperation instead of RestKit with this service is because I don't need to create any mapping for the returned response, I simply just want to save some of the returned data into local variables, but the problem is that service1 expects something like this JSON in the post body:
{
"trips": [
{
"departure_code": "SIN",
"arrival_code": "HKG",
"outbound_date": "2013-11-29",
"inbound_date": "2013-12-06"
}
],
"adults_count": 1
}
the question: how to create an AFJSONRequestOperation to post a request to service1 and send the above JSON with the post body ?
service2's question:
I want to use RestKit 0.2x to do a POST request to this service, I know how to build up the mapping model, but according to the service docs, I have to send a JSON object along with the post body that looks like this:
{
"id": "1376967853520",
"search_id": "IAXutjj0TAu0Wq-kvOMK6A",
"trip_id": "NYC:LON:2013-11-29:2013-12-06",
"fares_query_type": "route"
}
I used RestKit previously to do Get requests using getObjectsAtPath method, but in this case I think I have to use postObject method to do the POST request but I'm not sure of that.
the question: how to do a POST request to service2 using RestKit 0.2x and send the above JSON with the post body (considering that the mapping model is already set and ready to be used) ?
thank you so much for your kind help.

Create a dictionary and use NSJSONSerialisation (This would produce data for you to set as the operation payload). Or, use RKObjectManager requestWithObject:method:path:parameters: (which would create the URL request to send, with the payload data already set). Then create the request operation with that.
Yes, use the RKObjectManager postObject:... method. You need to create request mapping and descriptor for the class of object you're going to post. Then just post with the object and path.

Related

How to encode JSON as request parameter using Alamofire and AlamofireObjectMapper

I have a question about doing POST, PATCH, and PUT requests when using Alamofire and AlamofireObjectMapper. For example, lets say that I am doing a POST request to to send a object to a server. Can I pass a mapping as parameters for the request. Does it work similar to how RestKit encodes parameters into the body of the request as JSON?

iOS RestKit: How to make sure POST parameters are encoded in the request URL?

I'm trying to use RestKit because I'm expecting to make Core Data managed objects out of requests responses and it seemed like the framework was all about doing that and it seemed to be rather full featured.
But I'm having trouble getting my POST /user/login with parameters api_key=<value> (plus a separate JSON body) to end up a going out in a request like /user/login?api_key=<value>.
In the internals of RKObjectManager, requestWithMethod:path:parameters: does:
// NOTE: If the HTTP client has been subclasses, then the developer may be trying to perform signing on the request
NSDictionary *parametersForClient = [self.HTTPClient isMemberOfClass:[AFHTTPClient class]] ? nil : parameters;
request = [self.HTTPClient requestWithMethod:method path:path parameters:parametersForClient];
Do I have it right that this AFNetworking superclass method encodes parameters into the URL query? And does this mean the only way to ensure parameters are passed to that is to have my RKObjectManager use some subclass of AFHTTPClient?
And, according to the comment, supposedly this is only for sake of maybe a fringe case, something about request signing or something? Why is URL query-encoded request parameters not a common thing to do??
And getting things JSON encoded like I want does not seem to be as easy as I'd hoped either. Maybe it's a mistake for me to even try to use RestKit.

deployd - post method to retrieve object

Trying to use deployd.com framework for simulating a backend service. I am fairly new to deployd, appreciate any example or pointer in the right direction. Trying to retrieve an object using POST method. It looks like POST is used to create objects.
In my case, POST data contains a few query fields that will be used to query an object and send json back. Is this possible using deployd by modifying onPost method or some other way?
I cannot use GET with query parms due to security restrictions.
here is post request data
[
{
customer:"sam",
city:"noWhere",
}
]
the POST event should query by customer and city, then return matching customer object
[
{
customer:"sam",
postcode:"352345",
city:"noWhere",
country:"US"
}
]

using RestKit un-restfully

Is there any way to use RestKit in an unrestful way?
For example, If I set up an object mapping as such:
[manager.router routeClass:[BBMediaResourceCreate class]
toResourcePath:#"/mediaresources"
forMethod:RKRequestMethodPOST];
RestKit will expect that I post a BBMediaResourceCreate object and receive one back also.
My API however, for reasons I won't go into, is not RESTful compliant in some situations. Rather than receive the newly created resource, I'll get something more like:
{ Model: { Success:true} }
or something similar
Is there a way to map a RestKit post to post a resource of one type but expect a response of another type?
Thanks.
When using v0.10 you can simply set resourcePath:#"/"and respond to
- (void)objectLoaderDidLoadUnexpectedResponse:(RKObjectLoader *)objectLoader
or
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObject:(id)object
and handle the response in [objectLoader response] like you want. Keep in mind that posting to that resource then needs an explicit, manually set path.

How to pass array for a POST request in HTTPBuilder

The post request code that I use:
def http = new HTTPBuilder(uri)
http.request(Method.POST, ContentType.TEXT){
send ContentType.URLENC, attrs
..Response handler code...
}
here attrs is a map with the key&value that needs to be passed such as:
[param1:'value1', param2:'value2', param3:'value3]
I need to support passing multiple values for the same parameter, hence passing as a map is not an option. What is my alternative in this case? What I need to pass:
[param1:'value1', param1:'value2', param3:'value3']
You should be able to do:
send URLENC, [param1:['value1','value2'], param3:'value3']
Your example won't work as a map can't have multiple keys with the same name

Resources