1

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.}

user2769614
  • 247
  • 3
  • 6
  • 23
  • The accepted answer in this question should help: http://stackoverflow.com/questions/16476428/send-nested-json-using-afnetworking In short, the error is that the response of your server is not formatted as JSON. Check operation.responseString via NSLog. – Guto Araujo Nov 14 '13 at 00:23
  • @GutoAraujo this is what I got in responseString - Request does not have enough data – user2769614 Nov 14 '13 at 00:28
  • @user2916676 Are you sure that your PHP code is expecting a `application/x-www-form-urlencoded` request (which is what your code is creating)? Are you sure you have the field name correct? Can you share the PHP code with us? Also, focusing on the client-side for a sec, have you confirmed that `imageData` has the appropriate image data in it? – Rob Nov 14 '13 at 00:35
  • @user2916676 There are different techniques for PHP to accept file uploads (e.g. a `POST` that is a standard `application/x-www-form-urlencoded` format, custom web service that is expecting JSON or XML request with base 64 encoded string, etc.). We probably need more details about what your PHP is expecting (assuming, of course, that you've confirmed that your `imageData` is valid (i.e. non-`nil`)). – Rob Nov 14 '13 at 00:54
  • Strikes me that you need to get more information about the service to which you're submitting the image. I don't know how you can upload an image without knowing the particulars about the API. Either examples of well formed requests, API documentation, or someone who can get you a copy of the PHP source. – Rob Nov 14 '13 at 01:23

2 Answers2

3

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");.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • using this the error has gone but now I get Error: Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: not found (404) – user2769614 Nov 14 '13 at 01:08
  • @user2916676 404 means that the URL is wrong or otherwise not found. – Rob Nov 14 '13 at 01:24
0

Please check Follow the link for best and easy way to image uploading.

https://stackoverflow.com/a/49231873/5247430

Super Developer
  • 891
  • 11
  • 20