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
Related
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.
I am trying to upload a image using AFHTTPSessionManager through REST API call.
following are my code
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager.requestSerializer setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[manager POST:#"http://localhost:8888/uploadImage/100" parameters:nil constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFileData:imageData name:#"avatar" fileName:#"avatar.jpg" mimeType:#"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(#"success:%#", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(#"Error:%#", error);
}];
Response is going to field block and getting 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=0x9cbc690 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x9cbaf00 "Request failed: length required (411)"}
Any one have idea on this issue ?
Thanks
+(void)uploadImageData : (NSString *)url parameters:(NSDictionary *)dparameters imageData:(NSData *)dimageData success: (void (^) (NSDictionary *responseStr))success failure: (void (^) (NSError *error))failure
{
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
AFHTTPRequestOperation *op = [manager POST:url parameters:dparameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//do not put image inside parameters dictionary as I did, but append it!
[formData appendPartWithFileData:dimageData name:#"image" fileName:#"photo11.jpg" mimeType:#"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//success
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// failure
failure(error);
}];
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[op start];
}
The error
JSON text did not start with array or object and option to allow
fragments not set
Indicates that the server sent JSON with a fragment which isn't supported by the iOS JSON parser by default. You can enable it by setting the AllowFragments option for readingOptions on your AFJSONResponseSerializer.
Try to add (Swift)
manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments)
This solves for me.
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);
}];
}
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.
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?