0

I am working on an iOS app, using web-service. For call my web-service i decided to use AFNetWorking 2.0. And i have a big issue : On the command line i use curl :

    curl -X POST -d '[{"username":"user123","password":"querty"}]' -H "Content-Type:          application/json" http://mysite

This works perfectly.

Now when i want to use on iOS App:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];


    [manager.requestSerializer setValue:@"application/json; text/html" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json; text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    NSDictionary *parameters = @{@"username": @"user123", @"password":@"querty"};

    [manager POST:@"http://mysite" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

i got this error : Request failed: unacceptable content-type: text/html

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2724028
  • 594
  • 2
  • 8
  • 19
  • use this [manager.requestSerializer setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; // [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; NSLog(@"POST called with param : %@",parameter); [manager POST:requestURL parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) – Ganesh Kumar Sep 05 '14 at 04:42
  • thanks for your answer, but i have the same error. And i have this for the parameters : POST called with param : { password = querty; username = "user123"; } – user2724028 Sep 05 '14 at 07:12
  • try this.i think ur server is sending "text/html", and this type is not supported by default. Add @"text/html" for "acceptableContentTypes" manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; – Ganesh Kumar Sep 05 '14 at 07:16
  • i got this error : JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x8d3fc70 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x8d41610 "Request failed: unsupported media type (415) – user2724028 Sep 05 '14 at 07:23
  • I think your Json is the problem.[{"username":"value", "password":"value"}] – Ganesh Kumar Sep 05 '14 at 07:29
  • i think so, but passe it directly to the parameters to AfNetWorking. The web service need a parameter on json format. – user2724028 Sep 05 '14 at 07:31
  • Try this NSLog(@"POST JSON: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);NSError *error = nil; NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error: &error]; NSLog(@"%@",json); if(error){ if(self.delegate) [self.delegate requestFailedWithError:error forAPI:requestName]; }else{ if(self.delegate) [self.delegate parsedResponse:responseObject forAPI:requestName]; } – Ganesh Kumar Sep 05 '14 at 07:36
  • same error 415. Thanks for your help anyway. – user2724028 Sep 05 '14 at 08:06
  • check your key is in string format. – Ganesh Kumar Sep 05 '14 at 08:58
  • Check this link too.http://stackoverflow.com/questions/19874935/afnetworking-2-0-post-issue-cocoa-error-3840json-text-did-not-start-with-array?rq=1 – Ganesh Kumar Sep 05 '14 at 09:06
  • It always doesn't work. On android this very simple code above works perfectly. **HttpPost httppost = new HttpPost(uri[0]); httppost.setHeader("Content-Type", "application/json"); HttpPost post = new HttpPost(uri[0]); JSONObject json = new JSONObject(); json.put("username", "papa1@free.fr"); json.put("password", "aaaaaa"); StringEntity se = new StringEntity( json.toString()); se.setContentType(newBasicHeader(HTTP.CONTENT_TYPE,"application/json")); post.setEntity(se);** – user2724028 Sep 05 '14 at 09:42
  • I cant able to answer here.Otherwise i can post my code .Check this linkhttp://stackoverflow.com/questions/19186754/how-to-create-simple-http-request-via-afnetworking – Ganesh Kumar Sep 05 '14 at 09:58

0 Answers0