AFNetworking 2.0 POST request not working - ios7

I am trying to make a POST request using AFNetworking 2.0.
This is the code
NSMutableDictionary *params=[NSMutableDictionary dictionary];
[params setValue:#"iOS" forKey:#"device"];
[params setValue:#"" forKey:#"token"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:ServerURL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"%#",responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"%#",error);
}];
I have followed many question and they all have done something similar to what i have done above . I do not understand what i am missing. i get a valid response using the old Afnetworking library.
The error i get is
{ status code: 200, headers {
Connection = close;
"Content-Length" = 195;
"Content-Type" = "text/html; charset=UTF-8";
Date = "Mon, 21 Apr 2014 06:53:55 GMT";
Server = "Apache/2.2.25 (Amazon)";
"X-Powered-By" = "PHP/5.3.27";
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

I resolved it by adding
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
not sure if this is the right way , but it worked for me
Cheers.

Related

Post method using Afnetworking in Objective-C

How to write the code to sending POST method for the below JSON format using Afnetworking.
{
Media
{
Photo : image.jpg,
UserId : 2
},
Personal
{
Name : aaa,
Age : 30
},
Education
{
College : xxx,
Course : yyy
},
}
Obviously that's not quite the actual format, but we can guess what you might have meant. Clearly, if those numeric values (the user id and age) are expected as strings, then quote then, but hopefully it illustrates what the Objective-C representation of that dictionary might look like:
NSDictionary *parameters = #{#"Media": #{#"Photo": #"image.jpg", #"UserId": #2}, #"Personal": #{#"Name": #"aaa", #"Age": #30}, #"Education": #{#"College": #"xxx", #"Course": #"yyy" }};
Then you can post it as JSON like so:
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:#"https://yoururl.com" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(#"responseObject = %#", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(#"error %#", error);
}];
And if your server doesn't support HTTPS, but only HTTP, then you might have to alter your Info.plist's "App Transport Security Settings" to "Allow Arbitrary Loads".
Firstly make a dictionary of params you want to send
NSDictionary *params = #{
#"contact_no":#"9898989898",
#"password":#"••••••••••"
};
Then write the following code
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
operationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
[operationManager POST:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
NSLog(#"%#",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error){
if (failure)
NSLog(#"%#",error.localizedDescription);
}];
You can neglect the line in which i am setting the acceptableContentTypes.
Hope this will help.

Internal Error 500 when sending facebook photo urls >720p to server

I am trying to send photo urls which are of more than some 700 px . But when i am sending to the data through API . Some photos are uploading but the other were showing 500 internal error .
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:[#"Bearer " stringByAppendingString:[[NSUserDefaults standardUserDefaults]valueForKey:#"authKey"]] forHTTPHeaderField:#"Authorization"];
NSString *urlString=[API Link =%#",fbPic];
[manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSInteger statusCode = operation.response.statusCode;
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{ NSLog(#"Error: %#", error);
NSInteger statusCode = operation.response.statusCode;
}
];
and here is the error
status code: 500, headers {
"Cache-Control" = "no-cache";
"Content-Length" = 175;
"Content-Type" = "text/plain; charset=utf-8";
Date = "Wed, 29 Jun 2016 12:04:03 GMT";
Expires = "-1";
Pragma = "no-cache";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";}
HTTP 500 error indicate that the error comes from the server. You should check with the server developper to get more details

AFNetworking unauthorized 401 error in iOS 8

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];;
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:#"application/json"];
[manager POST:url
parameters:parameter
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response JSON: %#", data);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
Above code is working fine in iOS7 but I got follow error code in ios8.
status code: 401, headers
"Cache-Control" = "no-cache";
Connection = "keep-alive";
"Content-Type" = "application/json; charset=utf-8";
Date = "Mon, 10 Nov 2014 09:22:04 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
"Set-Cookie" = "request_method=POST; path=/";
"Transfer-Encoding" = Identity;
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "971366a5-f11d-4f3c-8a0f-0618905d43f4";
"X-Runtime" = "0.003365";
"X-XSS-Protection" = "1; mode=block";
Thanks in advance for your help.

How to change AFNetworking2.0 parameter encoding?

AFNetworking2.0 encodes parameters with UTF8. How can I change AFNetworking 2.0's parameter encoding to gb2312?
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
That encoding is gb2312, but how to add it to AFNetworking?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
NSDictionary *parameters = #{#"__VIEWSTATE":#"dDwtMTg3MTM5OTI5MTs7PkDQD2kYQWAxp4gTWKdd1YunUJ%2B%2B",#"TextBox1": self.xueHao.text,#"TextBox2":self.miMa.text,#"TextBox3":self.yanZhengMa.text,#"RadioButtonList1":#"%D1%A7%C9%FA"};
[manager POST:#"http://172.21.96.64/default2.aspx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);//提交表单
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", #"???");
}]; }
Response Headers
Cache-Control:no-cache, no-store
Content-Length:5628
Content-Type:text/html; charset=gb2312
Date:Sun, 16 Feb 2014 14:00:14 GMT
Expires:-1
Pragma:no-cache
Pragma:no-cache
Server:Microsoft-IIS/6.0
X-AspNet-Version:1.1.4322
X-Powered-By:ASP.NET
After digging around in the source code, it looks like AFHTTPRequestOperationManager has a property for the request serializer - which then has a property for the string encoding.
So, you should be able to do this:
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
RequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
manager.requestSerializer.stringEncoding = enc;
NSDictionary *parameters = #{#"__VIEWSTATE":#"dDwtMTg3MTM5OTI5MTs7PkDQD2kYQWAxp4gTWKdd1YunUJ%2B%2B",#"TextBox1": self.xueHao.text,#"TextBox2":self.miMa.text,#"TextBox3":self.yanZhengMa.text,#"RadioButtonList1":#"%D1%A7%C9%FA"};
[manager POST:#"http://172.21.96.64/default2.aspx" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);//提交表单
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", #"???");
}]; }
Note that I haven't had a chance to test this yet, but from looking at the source code I'm pretty sure it will work. Confirmation would be appreciated.

Post Request with AFHttpRequestOperationManager not working

I am using AFHTTPRequestOperationManager to post some JSON to my server, my code is below.
NSDictionary *parameters = [[NSDictionary alloc] initWithObjectsAndKeys:#"john", #"name", #"xxxxx#gmail.com", #"email", #"xxxx", #"password", #"1", #"type", nil];
// Do any additional setup after loading the view.
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
[operationManager POST:#"posturl here" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", [responseObject description]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", [error description]);
}];
and the response is as follows:
2013-11-18 16:49:29.780 SwapOnceApiTester[12651:60b] Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unsupported media type (415), got 1664256" UserInfo=0x1565a6c0 {NSErrorFailingURLKey=xxxxxxx, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x15656db0> { URL: xxxxxxxxx } { status code: 415, headers {
"Cache-Control" = "max-age=604800";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Mon, 18 Nov 2013 11:49:28 GMT";
Expires = "Mon, 25 Nov 2013 11:49:28 GMT";
Server = nginx;
"Transfer-Encoding" = Identity;
"X-Powered-By" = PleskLin;
} }, NSLocalizedDescription=Request failed: unsupported media type (415), got 1664256}
I dont know what the problem is with this.
You need to set your request and response serializers to handle JSON using AFJSONRequestSerializer and AFJSONResponseSerializer before executing your request. Using an NSDictionary literal for your parameters helps code clarity as well:
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[operationManager POST:#"posturl here"
parameters: #{#"name": #"john",
#"email": #"xxxxx#gmail.com",
#"password: #"xxxxx",
#"type": #"1"}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", [responseObject description]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", [error description]);
}
];
Just solve my problem, my server was not configured to accept the charset utf8 with application/json so i just removed the charset utf for ajson serializer in Afnetworking 2.0 and now it is working correclty.