How to add raw body in AFHTTPSessionManager request? - ios

I want To add Raw body in my AFHTTPSessionManager request

AFHTTPRequestOperationManager *manager;
manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setTimeoutInterval:10.0];
[manager POST:strRequestUrl parameters:dictParams success:^(AFHTTPRequestOperation *operation, id responseObject)
{
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];

Related

Send Asset Video to web service - iOS

I am trying to send video in assets library to web service with parameter video but how is it possible to do ?
I got the NSURL as the following : assets-library://asset/asset.MOV?id=What-ever-0000-000&ext=MOV
I am using AFNetworking to send the post request and here is my code :
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]];
[manager.requestSerializer setValue:#"no-cache" forHTTPHeaderField:#"Catch-Control"];
NSDictionary *parameters = #{#"video": myurl};
[manager POST:#"this is my service link" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Do I need to convert the NSURL to another representation so I can submit it? I have no idea?
You can post video to server like this.
AFHTTPRequestOperation *op = [theManager POST:urlString
parameters:theParameters
constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileURL:postedVideoURL name:#"media" fileName:#"FinalMovie.mp4" mimeType:#"mp4" error:nil];
[formData appendPartWithFileData:imgData name:#"video_thumb" fileName:#"png" mimeType:#"image/jpeg"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{}];
[op start];
Create theManager object like this.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
Hope this helps.

AFNetworking response failure block is called instead of success block

AFNetworking response failure block is being called when I get status code 200. How can I make the success be called instead?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://128.199.94.58/test/bt/client_token.php" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.clientToken = responseObject[#"customerID"];
NSLog(#"Client Token received.");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle failure communicating with your server
NSLog(#"Client Token request failed.%#",operation.responseString);
NSLog(#"error code %ld",(long)[operation.response statusCode]);
}];
Look at the value of error. It will tell you why the connection failed. "Failure" in this context has nothing to do with the status code. Returning "404" is still a "success." Failure means you were unable to complete the operation.
use acceptableStatusCodes as follows:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [TimeoutAFJSONRequestSerializer serializer];
NSMutableIndexSet* codes = [[NSMutableIndexSet alloc] init];
[codes addIndex: 200];
manager.responseSerializer.acceptableStatusCodes = codes;
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];
I run this code and it work find.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:#"http://128.199.94.58/test/bt/client_token.php" parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseObject
options:kNilOptions
error:nil];
self.clientToken = json[#"customerID"];
NSLog(#"Client Token received.");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Handle failure communicating with your server
NSLog(#"Client Token request failed.%#",operation.responseString);
NSLog(#"error code %ld",(long)[operation.response statusCode]);
}];
responce is:
json:
{
customerID = "eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJhMjg2OGVjY2FmZjNjMTQ0M2Y4MTg2MjQ4NDFhZDIyZGM3MWFh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3NldHNVcmwiOiJodHRwczovL2NoZWNrb3V0LnBheXBhbC5jb20iLCJkaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwibWVyY2hhbnRBY2NvdW50SWQiOiI2ejl3eGtkanlyNnQzbmg1IiwiY3VycmVuY3lJc29Db2RlIjoiVVNEIn0sImNvaW5iYXNlRW5hYmxlZCI6ZmFsc2UsIm1lcmNoYW50SWQiOiJ6cWQ5ZHBqZmZkc2s3eG55IiwidmVubW8iOiJvZmYifQ==";
}
It may be work for you.
If you check your error in failure block it clearly say that invalid content type. You need to set the content type of the manager as follows
manager.requestSerializer = [AFJSONRequestSerializer serializer];
try this
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
and in success block
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:manager.responseData options:kNilOptions error:nil];
self.clientToken = dic[#"customerID"];
NSLog(#"Client Token received.");
}

How to cache response in AFHTTPRequestOperationMnager

This is my code. I set cachePolicy = DontLoad, but it still get data from server not from local cache. Why?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer.cachePolicy = NSURLRequestReturnCacheDataDontLoad;
AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.responseSerializer = responseSerializer;
[manager GET:#"http://www.github.com" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#", [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];

Pass Json string in AFNetworking 2.0

i want to pass json string in AFNetworking i am using the code below to parse the data to the server.
My JSON String :
{"Queries":[{"Field":"text_all","Value":"Class1","IsPhrase":false,"Near":0}],"Filter":[{"Key":"Test","Value":["Test","Test2"]}],"Paging":0,"Count":10}
Code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:#“http://www.test.com” parameters:main_dict success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"responseString: %#", json);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
}];

How can I add a request header with AFNetworking?

I want to use AFNetworking class to my application to communicating with server.
I am new to this library . Here is my code:
NSDictionary *user=[[NSDictionary alloc]initWithObjectsAndKeys:#"hiteshp",#"userName",#"12345^",#"password", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
[requestSerializer setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
manager.requestSerializer=requestSerializer; //[AFJSONRequestSerializer serializer];
[manager POST:#"myURL" parameters:user success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %# %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I want to add header to this request . I tried but it is not working . If I wrote commented line then request is done but I also want to add one more field "Authorization" to requset.
Andhow to print request header?
I know it's too late but here's a code that makes what you want
NSString *finalyToken = [[NSString alloc]initWithFormat:#"Bearer %#",user.token];
AFHTTPRequestOperationManager *manager =
[[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:#"https://server1.appscserver.de/api"]];
[manager.requestSerializer setValue:finalyToken forHTTPHeaderField:#"Authorization"];
Hope it helps someone
Use this in your code:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"text/html", #"application/json", nil];
//Authorization
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:userName password:password];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
in php file add:
header('Content-type: application/json');
in xcode:
manager.responseSerializer = [AFJSONResponseSerializer serializer];
example xcode code:
NSDictionary *parameters = #{#"key1": #"value1",
#"key2": #"value2"};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:[NSString stringWithFormat:#"http://%#", Url] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

Resources