Cocoa error 3840 - ios

I'm trying send post request to server with AFNetworking 2.0
NSDictionary * dict = #{#"credentials": #{
#"name": #"Valod",
#"password": #"test"
}
};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:URL parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];`
but I'm getting the error
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x7fd6214a5d70 {NSDebugDescription=Invalid value around character 0., NSUnderlyingError=0x7fd62150ec60 "Request failed: internal server error (500)"}
I test api call with google application.And the response json is valid json

Check your requestSerializer, maybe it 's AFHTTPRequestSerializer instead of AFJSONRequestSerializer.
Try setting manager.requestSerializer = [AFJSONRequestSerializer serializer];

Related

Domain=NSCocoaErrorDomain Code=3840 error in afnetworking 2.0

i am unable to find solution of NSCocoaErrorDomain Code=3840 error in afnetworking 2.0
this is code i used
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:#"text/html"];
[manager POST:url parameters:inputs success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"JSON: %#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
this is the error getting
Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
Try to use AFHTTPResponseSerializer instead
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
Because you're using acceptableContentType of #"text/html", I assume you don't need a JSON serializer on response call. So this should be fine for your case.

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed using AFNetworking

I am using AFNetworking library to post data on server using POST method.
Following is my code
- (void) callLoginAPI:(NSDictionary *)dictProfile{
// 1
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:[dictProfile valueForKey:#"name"], #"username",
[dictProfile valueForKey:#"first_name"],#"first_name",
[dictProfile valueForKey:#"last_name"],#"last_name",
[dictProfile valueForKey:#"email"],#"email",
[dictProfile valueForKey:#"birthday"],#"dob",
[dictProfile valueForKey:#"gender"],#"gender",
[[dictProfile valueForKey:#"location"] valueForKey:#"name"],#"location",
[dictProfile valueForKey:#"timezone"],#"timezone",
#"",#"language",
[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture?type=large",[dictProfile valueForKey:#"id"]],#"profile_pic_url",
#"",#"cover_pic_url",nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:#"http://10.1.81.35:8000/api/login/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
but I got following error in response
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x797f2620 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I can't understand what is the problem with the code.
The problem comes from response parsing.
You are trying to de-serialize a JSON reponse (which MUST be contained in either a NSArray or NSDictionary) however your response is none of the above (Most likely a simple string).
Also, try to set the "allow fragments" to the response serializer.
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
May be you need authentication to access JSON response. Set authentication like that:
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"XYZ" password:#"xyzzzz"];
Try this:
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[self setResponseSerializer:responseSerializer];
instead of:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//Request Serializer
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//Response Serializer
AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = responseSerializer;

JSON error in AFNetworking

I'm trying to use the AFNetworking library to send a post request to an URL like this:
https://m.com/api/2.0/basic/sim_balance.json
That expects a username and password in the URL that are my parameters (as NSDictionary).
The URL gives a JSON back like this:
{
"valid_until": "2011-05-05 22:13:28",
"sms": 0,
"sms_super_on_net_max": 300,
"voice_super_on_net": 0,
"credits": "0.00",
"bundles": [],
"is_expired": true,
"sms_super_on_net": 0,
"voice_super_on_net_max": 3600,
"data": 0
}
This is my request function:
+(void) request:(NSString *)endpoint : (NSDictionary *) parameters
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:endpoint parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
}
I always get this as error:
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x904b7e0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I validated the JSON and the server is sending valid JSON.
Can someone help me out on this?
To get AFHTTPRequestOperationManager to serialize the request body as JSON, you need to set the value of its requestSerializer:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:....];
If you do not set the requestSerializer property, then the manager object will use x-www-form-urlencoded serialization by default.
Note however that AFHTTPRequestOperationManager will handle JSON in the response body by default.
I found the solution thanks to ester. Also you need to set the basic auth headers right.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"Tom" password:#"test"];
[manager GET:endpoint parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Response data: %#",operation.responseData);
NSLog(#"Response String: %#",operation.responseString);
NSLog(#"Error: %#", error);
}];
}

Error in parse JSON using AFNetworking 2.0 when a new line character comes in response with data

I am facing some problem in parsing JSON using AFNetworking 2.0. It always executes the failure block when a new line character comes with response data.
My Code -
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager GET:#"http://mobile.MYSERVICENAME/outbox.aspx?u=SONALI2547&p=25472870&sender=sjssgn" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(#"%#", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(#"%#", error);
}];
ERROR -
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be
completed. (Cocoa error 3840.)" (Unescaped control character around
character 512.) UserInfo=0xa279660 {NSDebugDescription=Unescaped
control character around character 512.}
I am check I am check my url on jsonlint it give me above error.
I had the same problem. The problem is probably related with the way of the encoding and the decoding of the serve. Replace "\n" to "\n" in your JSON, then it will be fine.

iOS : .Net API (asmx) Post Request With AFNetworking + AFHTTPRequestOperationManager

Hie all, I'm trying to send a post request on .net (asmx) API using AFNetworking class AFHTTPRequestOperationManager, here's my code that doing request,
-(void) request {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = #{#"test1": #"123",
#"test2": #"345"};
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/xml"];
[manager POST:#"http://somewebsite.com/getdetail.asmx/AllDetails" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSLog(#"%#",operation.description);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
NSLog(#"%#",operation.responseString);
}];
}
I'm getting the following response string
{"result":[],"status":"SUCCESS"}
And it prints the following error,
Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x996a630 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
What's going wrong?
How do I send post request using AFNetworking, to .net asmx API, is that a correct way?

Resources