"Expected status code in (200-299), got 404" - ios

This is my code:
NSURL *url = [NSURL URLWithString:#"http://wspublisherv2.skygiraffe.com/WSpublisherV2.svc/Authenticate"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:#"john#sgdemo.com", #"UserName", #"123", #"Password", nil];
NSLog(#"%#", parameters);
[client postPath:nil parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"success: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"failure: %#", error);
}];
It always triggers the failure block and I get the "Expected status code in (200-299), got 404" message.
When I try it through Fiddler it works.

You need more information. Use a proxy like Charles Proxy to watch the traffic between your device and the server. That'll let you see the actual request. You can compare that to a request that works, and the difference should give a pretty good idea of what's wrong. At the very least, it'll make your question much more specific.

Related

Afnetworking request get time out on iPhone 5 , iPhone 6 and iPhone 6+ but works on iPhone 4

I have the following URL:
http://beta.bemembr.com/en/webservice/abc123/members/login/email/******/password/****
And this piece of code:
NSString *path = [NSString stringWithFormat:#"en/webservice/%#/members/login/email/%#/password/%#",apiKey,login,pass];
NSLog(#"PATH IS %#",path);
[manager GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject)
{
NSDictionary *json = (NSDictionary *)responseObject;
NSLog(#"JSON IS %#",json);
int status = [[json valueForKey:#"status"] intValue];
if(status == 200){
compblock(YES);
}else{
compblock(NO);
}
}failure:^(NSURLSessionDataTask *task, NSError *error)
{
// Failure
NSLog(#"Failure: %#", error);
compblock(NO);
}];
This is working on an iPhone4 but fails on iPhone 5, iPhone6 and iPhone 6+
This is the error I got:
Failure: Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f8951c5c7b0 {NSErrorFailingURLStringKey=http://beta.bemembr.com/en/webservice/abc123/members/login/email/*****/password/*****, NSUnderlyingError=0x7f8951c82220 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey=http://beta.bemembr.com/en/webservice/abc123/members/login/email/*****/password/*****}
Can anybody help me?
Thanks !
Try avoiding the username and password component of your URL, and try something like this instead.
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://examplewebsite.com]];
[client setAuthorizationHeaderWithUsername:#"username" password:#"password"];
Use AFNetworking: [AFNetworking2.0] https://github.com/AFNetworking/AFNetworking
Add UIKit+AFNetworking and AFNetworking folder into your project.
and add this code in the file you want to use AFNetworking:
#import "AFnetworking.h"
#import "UIKit+AFNetworking.h"
And use this code:
-(void)login
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"some_url" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"Error: %#", error);
}];
}
Since you are passing parameters within the URL String, set it to nil: parameters:nil

Uploading images to server

I am trying to upload an image to a server (that is already built) and I am getting errors like Request has timed out. Other methods of sending text and fetch data from the server are working properly. However, sending an image I found it hard to do it.
I am using the following code at the moment:
-(void)uploadImage:(NSData*)image callbackBlock: (void (^)(BOOL success)) callbackBlock
{
NSString *path = [NSString stringWithFormat:#"upload"];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, #"image", nil];
[params addEntriesFromDictionary:self.sessionManager.authParameters];
NSMutableURLRequest *request = [self multipartFormRequestWithMethod:#"POST" path:path parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData){
[formData appendPartWithFormData:image name:#"Image"];
}];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"!!!Response object: %#",responseObject);
callbackBlock(YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Failure: %#",error.description);
callbackBlock(NO);
}];
[self enqueueHTTPRequestOperation:operation];
}
Do you have any idea what the problem is? Can you give me some suggestions or possible errors on the above code.
Thank you very much.
You can send your image as a base64 encoded text... This should work.
You can use this category to create base64 encoded image:
https://github.com/l4u/NSData-Base64

POST with AFNetworking, error code -1001

I want to POST a json message to send to a server. I don't have access to the server itself but I know it is working and set up to return a success/failure message when a message gets through. The server requires an app id and password to connect
I am using AFNetworking for this. Here is the code I'm using:
NSURL* pushServerURL = [NSURL URLWithString: #"http://myserverurl.com/"];
AFHTTPClient* networkInstance = [[AFHTTPClient alloc] initWithBaseURL: pushServerURL];
NSDictionary *parameters =[NSDictionary dictionaryWithObjectsAndKeys:
#"myappid", #"app_id", #"mypassword", #"password", nil];
NSDictionary *params =[NSDictionary dictionaryWithObjectsAndKeys:
parameters, #"user", nil];
[networkInstance postPath: #"users/login.json"
parameters: params
success:^
(AFHTTPRequestOperation *operation, id jsonResponse)
{
NSLog (#"SUCCESS");
}
failure:^
(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"FAILED");
}];
It always fails with the error code -1001.
The NSError has 4 keys in the user info
NSErrorFailingURLStringKey with value #"http://myserverurl.com/,
NSErrorFailingURLKey with value <not an Objective-C object>,
NSLocalizedDescription with value "The request timed out"
NSUnderlyingError and the value has no string.
Any idea what I might be doing wrong?
Try this line of code. You can easily find what is the response coming from server or else, where the actual problem is?
[networkInstance postPath: #"users/login.json"
parameters: params
success:^(AFHTTPRequestOperation *operation, id jsonResponse) {
NSLog (#"SUCCESS");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#" Error - Statistics file upload failed: \"%#\"", [request error]);
}];

AFHTTPClient Expected status code in (200-299), got 409

I have strange issue with AFHTTPClient, I am sending POST request like
NSURL *u = [NSURL URLWithString:HTTP_SERVER];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL: u];
[httpClient setParameterEncoding:AFJSONParameterEncoding];
[httpClient postPath:REGISTER
parameters:params
success:^(AFHTTPRequestOperation *operation, id JSON) {
int statusCode = [operation.response statusCode];
if(statusCode == 201){
[self performSegueWithIdentifier:#"register" sender:self];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"[HTTPClient Error]: %#", error.localizedDescription);
}];
And server works fine ( in some cases server needs to answer me with code 409, API is made in that way), but I get error in XCode like Expected status code in (200-299), got 409
How to solve this problem ( my hands are tied, I cannot change API and error code) ?
Based on this:
You've got two options:
add status code 409 to the list of acceptable status codes, and receive it in the success block (bad)
deal with status code 409 in the failure block (good)
For the latter, if your [operation.response statusCode] is 409, you would use the responseData from AFURLConnectionOperation (superclass of AFHTTPRequestOperation, you would need to import its header for this to work).

AFNetworking and 404

I am trying to post to a URL using AFNetworking and no matter what I do I keep getting the error:
Error Code: -1011 - Expected status code in (200-299), got 404
My code is as follows:
NSString *baseurl = #"http://mysiteurl";
NSString *path = #"/user/register/";
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseurl]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
//[client setAuthorizationHeaderWithUsername:#"myusername" password:#"mypassword"];
[client postPath:path parameters:[NSDictionary dictionaryWithObjectsAndKeys:_userName,#"user", _email, #"email",_password,#"password", nil] success:^(AFHTTPRequestOperation *operation, id JSON) {
//NSLog(#"sjson: %#", [JSON valueForKeyPath:#"entries"]);
NSLog(#"sjson: %#", JSON);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error Code: %i - %#",[error code], [error localizedDescription]);
}];
When I go to http://mysiteurl/user/register/ directly I am able to see JSON.
What am I doing wrong?
When I go to http://mysiteurl/user/register/ directly I am able to see JSON.
If you're doing this through a browser, you are making a GET request, whereas in your code, you are making a POST request.
A 404 is not just the visible address, it includes the HTTP method as well. You need to make sure that your server responds to a POST at http://mysiteurl/user/register/. Depending on your framework (e.g. Rails), you may have to add [client setDefaultHeader:#"Accept" value:#"text/json"] to get the correct route.
i usually use [client getPath: parameters:params success:] not Post but i guess this will work for both cases since your response is in a JSON format too
add these two lines
[client.parameterEncoding = AFJSONParameterEncoding;
[client setDefaultHeader:#"Accept" value:#"text/json"];
before
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];

Resources