Domain=NSCocoaErrorDomain Code=3840 error in afnetworking 2.0 - ios

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.

Related

Cocoa error 3840

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];

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.

image upload using AFNetworking, ios

I have to upload image to server by sending bytes of photo and I'm using AFNetworking,
but I keep getting this 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=0x10abb7350 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
If the PHP server is not responding with application/json, then you have to tell your manager to accept general HTTP responses:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:#"image" fileName:imageFilename mimeType:#"image/png"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"Success: %#", string);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
If you server is responding with JSON response, then perhaps it's not setting the Content-Type header properly, e.g., header("Content-type: application/json");.
Please check Follow the link for best and easy way to image uploading.
https://stackoverflow.com/a/49231873/5247430

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