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.
Related
NSURL *URL = [NSURL URLWithString:#"some APi"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
UIImage *myImageObj = [UIImage imageNamed:#"avatar.jpg"];
NSData *imageData= UIImagePNGRepresentation(myImageObj);
[manager POST:URL.absoluteString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:#"file"
fileName:#"avatar.jpg" mimeType:#"image/jpeg"];
// etc.
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"Response: %#", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am trying to upload image using afnetworking 3.0 but getting this error every time
Domain=com.alamofire.error.serialization.response Code=-1011 "Request
failed: internal server error (500)"
UserInfo={com.alamofire.serialization.response.error.response= { URL: Some URL } { status
code: 500, headers {
"Access-Control-Allow-Origin" = "*";
"Content-Length" = 291;
"Content-Type" = "text/html";
Date = "Thu, 26 Jan 2017 11:41:19 GMT";
Server = "Werkzeug/0.11.11 Python/2.7.12"; } },
luckily postman provide objective-C and some other languages code but with AFNetworking i used this
NSURL *URL = [NSURL URLWithString:#"your URL"];
UIImage *myImageObj = [UIImage imageNamed:#"image.jpg"];
NSData *imageData= UIImageJPEGRepresentation(myImageObj, 0.6);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//manager.responseSerializer=[AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager.requestSerializer setValue:#"multipart/form-data" forHTTPHeaderField:#"Content-Type"];
[manager POST:URL.absoluteString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData
name:#"file"
fileName:#"image.jpg" mimeType:#"image/jpeg"];
// etc.
} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"Response: %#", responseObject);
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#",string);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error: %#", error);
}];
I am trying to build mac osx application For UBER.I have completed all steps of Aouth2. I am successfully getting access_token. I am also able to retrieve user profile and history but while I am trying to post "ride request", I am getting 401 unauthorized error response.please help.Thank you In advance.
My code is as below.
**
//POST /v1/requests
NSString *sandBoxURL=#"https://sandbox-api.uber.com/v1";
NSString *url = [NSString stringWithFormat:#"%#/requests", sandBoxURL];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request addValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:[NSString stringWithFormat:#"Bearer %#", _accessToken] forHTTPHeaderField:#"Authorization"];
NSError *error = nil;
request.HTTPMethod = #"POST";
request.HTTPBody = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
NSLog(#"Request for Product Request:%#",request);
[self performNetworkOperationWithRequest:request completionHandler:^(NSDictionary *requestDictionary, NSURLResponse *response, NSError *error)
{
NSLog(#"Response for Product Request:%#",response);
NSLog(#"Result:%#",requestDictionary);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300)
{ //OK
UberRequest *requestResult = [[UberRequest alloc] initWithDictionary:requestDictionary];
// handler(requestResult, response, error);
}
if (409 == httpResponse.statusCode) { //needs surge confirmation
NSLog(#"Surge Conflict");
}
else
{
NSLog(#"Error In response");
}
}];
**
And response I am getting is:
{ URL: https://sandbox-api.uber.com/v1/requests } { status code: 401, headers {
Connection = "keep-alive";
"Content-Length" = 83;
"Content-Type" = "application/json";
Date = "Mon, 07 Nov 2016 11:19:35 GMT";
Server = nginx;
"Strict-Transport-Security" = "max-age=0";
"X-Content-Type-Options" = nosniff;
"X-Uber-App" = "uberex-sandbox, migrator-uberex-sandbox-optimus";
"X-Uber-Missing-Scopes" = true;
"X-XSS-Protection" = "1; mode=block";
} }
Result:{
code = unauthorized;
message = "Requires at least one scope. Available scopes: ";
}
I got the solution. Problem Was With My Scope input.While requesting for token,even if the account you are login is of developer, we need put scopes properly. In my case I needed request scope in token.
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
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.
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.