1

I have to make parameter like this

array( 
‘token’,
‘data’ => array( ‘name’,
                 ‘email’,
                 ‘password’,
  ) 
)

As I am a beginner to use REST API service. So I can't able to make this. I tried the following way to request

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = @{@"name" : name,
                             @"email" : email,
                             @"password" : password};
    [manager POST:BASE_URL  parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

Got a respose like this

Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x7f965071adc0 {AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7f9650721f80> { URL: BASE_URL } { status code: 200, headers {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html";
    Date = "Tue, 15 Sep 2015 16:33:10 GMT";
    Server = "nginx/1.8.0";
    "Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html, NSErrorFailingURLKey=BASE_URL

Is there any mistake to POST the request for sign up? What is the correct way to request? Please help.

2 Answers2

0

Try this

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//edited
   NSDictionary *innerDict = @{@"name":name,@"email":email,@"password":password};
NSDictionary *dict = @{@"data":innerDict};
NSArray *parameter = @[@token,@dict];
[manager POST:BASE_URL  parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//add this line

Include the last line i added.There might be some syntax error coz i am in a windows machine now, but xcode will auto complete for you, if you write it down on your own.

That should do it now.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • After doing this, I am getting the following 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=0x7fc37bdf9d10 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} – Chowdhury Md Rajib Sarwar Sep 15 '15 at 16:58
  • write this code inside the failure block `NSLog(@"%@",operation.responseString);` and see what is it printing? – Saheb Roy Sep 15 '15 at 17:00
  • getting some html and CSS code onclick="document.getElementById('cakeErr55f84f20d734e-trace').style.display = (document.getElementById('cakeErr55f84f20d734e-trace').style.display == 'none' ? '' : 'none');">Warning (2): json_decode() expects parameter 1 to be string, array given [APP/Plugin/Api/Controller/ApiAppController.php, line 39] – Chowdhury Md Rajib Sarwar Sep 15 '15 at 17:04
  • That means the server is NOT returning a JSON, and the data the server is returning must start with an array and by the recent comment you posted it means in your ApiAppController.php file Line number 39 there seems to be some mistake. Please rectify it. – Saheb Roy Sep 15 '15 at 17:10
  • @SahebRoy check the parameters, which are expecting from the server side, they are not accessing the parameters as key-value pair, they are accessing them as array. – Rajat Sep 15 '15 at 17:14
  • AFNetworking converts them into JSON before sending them which is wrapped into an string array format before it hits the server. – Saheb Roy Sep 15 '15 at 17:21
  • there is difference between jsonobject and jsonarray. – Rajat Sep 15 '15 at 17:26
  • Yes i just noticed the structure he needed and normally in any REST service a structure such as this isnt recommended, anyways am editing the answer accordingly. – Saheb Roy Sep 15 '15 at 17:29
0

Use this for parameters:

NSString *token = // Fill token
NSString *name = // Fill Name
NSString *email = // Fill Email
NSString *password = // Fill Password
NSDictionary *params = @[token,@{@"data": @[name,email,password]}];
Lokesh Dudhat
  • 449
  • 3
  • 10