unable to register an user using afnetworking

216 views Asked by At

I want to create a user to a back-end server, I need to enter following information to register user.

 {
  "user": {
    "email": "[email protected]",
    "password": "secret",
    "password_confirmation": "secret",
    "seed_recipes": true                 //optional
  }

I wrote following code for registering a user:

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSDictionary *parameters = @{@"user[email]":email, @"user[password]":password, @"user[password_confirmation]":password};
    [manager POST:@"http://domain.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

I got following errors. Does any body know the reason? (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fbe935cb360 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x7fbe935c54b0 "Request failed: unauthorized (401)"}

2

There are 2 answers

0
Grzegorz Krukowski On BEST ANSWER

Probably your server doesn't respond as JSON format just pure HTML.

Try adding:

[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

If that doesn't help, make sure your server responds in JSON format to POST you are trying to make.

0
Akshay Karanth On
NSDictionary *users=[[NSDictionary alloc] initWithObjects:@{@"email":email, @"password":password, @"password_confirmation":password}];

NSDictionary *parameters = [[NSDictionary alloc] initWithObjects:@{@"user":users}];

Try this