POST arbitrary text with AFNetworking - ios

I have seen how you can POST urlencoded forms and json using HttpClient.
But how do I POST arbitrary text?
My use case is that I already have my JSON string created, so I want to post the string directly, instead of constructing a dictionary.

Set the HTTPBody property on NSMutableURLRequest, and use anywhere in AFNetworking that takes an NSURLRequest parameter.

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?

Difference between AFJSONRequestSerializer and AFHTTPRequestSerializer

What actually is the reason that my request works for AFJSONRequestSerializer and doesnt for AFHTTPRequestSerializer?
I have set
responseSerializer.acceptableContentTypes?.insert("application/json")
for AFHTTPRequestSerializer and it doesn't work, but should.
I just send simple dictionary:
{
"approval_required" = 1;
birthdate = "2016-03-11";
gender = male;
name = Bartolo;
password = password1234;
username = "super user";
}
Here is similar question, but there is no explanation WHY it is like that.
Can anyone explain?
The acceptableContentTypes has little to do with the format of a request, ie whether it is HTTP request or JSON request. A request serializer's fundamental responsibility is to create a request in the specified format. Thus, the choice of requestSerializer dictates whether you are creating a HTTP request (e.g. a Content-Type of application/x-www-form-urlencoded) or JSON request (e.g. a Content-Type of application/json). The acceptableContentTypes merely allows the request to specify in what format(s) your app will accept responses. (And, frankly, you shouldn't specify acceptableContentTypes, but rather just use the correct responseSerializer.) But the request format and the accepted response format are two completely different things. In fact, it's not uncommon to have a web service that only accepts HTTP requests, and only provides JSON responses.
In your case, it sounds like you have a web service that is expecting a JSON request. When you specify AFHTTPRequestSerializer, the request will not be in JSON format and will likely not be understood. And specifying acceptableContentTypes does not alter the fact that the request, itself, isn't JSON.
By the way, tools like Charles or Wireshark are very useful for debugging network code, and would be illuminating in diagnosing the difference in these two types of requests. If you use one of these tools to watch the raw requests/responses, the difference will jump out at you.

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.

posting JSON object to a Restful web service

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.

AFNetworking: GET with query string

I´m trying to access a web service from my iOS application. In the documentation it says that I should use a query string to pass the parameters but I´m not sure on how to use this.
I using the AFNetworking framework. Made a POST request earlier and it worked just fine, but not sure on how to write and pass a query string.
The 'query string' is produced by AFNetworking. Just use the getPath:parameter:... method and provide the parameter dictionary. Make sure the dictionary has a key for each of the query names required by the web service.
For example:
[client getPath: #"transactions"
parameters: #{ #"api_key" : ksomeAPIKey,
#"user_id" : user_id }
...]
will be converted, by AFNetworking, to:
GET <baseURL>/transactions?api_key=...&user_id=...

Resources