0

I have no idea about Ruby on rails I have a task to create web-service with Ruby on rails. I am trying with AFNetworking and NSURLConnection but did not got responce.

TIMESTAMP_KEY = '6ad3e2c0ffa4a89217e0'
SECRET_KEY  = 'fd83be9e64ddec6ee8344708a189d039ad9e9d51'
HOST = 'http://em.dev.apb.com
URL = 'company_users/login'

They said above parameters is predefine and its ruby code like:

1 params = []
2 sorted_params = params.sort.join
3 puts "sorted_params=#{sorted_params}"
4 digest = Digest::SHA1.hexdigest("#{timestamp}#{TIMESTAMP_KEY}")
5 puts "digest=#{digest}"
6 puts "SHA1(#{URL}#{sorted_params}#{SECRET_KEY}#{digest})"
7 signature = Digest::SHA1.hexdigest("#{URL}#{sorted_params}#{SECRET_KEY}#{digest}")

puts "#{HOST}#{URL}"

response = RestClient.post "#{HOST}/#{URL}/#{signature}/#{timestamp}", :email => email, :password => password

I have no idea how to get its Response in objective C:

I am trying AFnetworking

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSString *signature =[self sha1:@"fd83be9e64ddec6ee8344708a189d039ad9e9d51"];
    NSString *timestep =[self sha1:@"6ad3e2c0ffa4a89217e0"];

    NSDictionary *params = @{@"email": @"abc@superuser.com",@"password": @"aaa111"};

    NSLog(@"%@",params);

    NSString *url =[NSString stringWithFormat:@"http://em.dev.abc.com/company_users/login/%@/%@",signature,timestep];

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

I got this Link

So I use this method

-(NSString*) sha1:(NSString*)input
{
    const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *data = [NSData dataWithBytes:cstr length:input.length];

    uint8_t digest[CC_SHA1_DIGEST_LENGTH];

    CC_SHA1(data.bytes, data.length, digest);

    NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];

    return output;

}

Every time I get this output as an Error like:

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=0x8c56190 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.

Xan
  • 74,770
  • 16
  • 179
  • 206
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • Take look at http://stackoverflow.com/questions/10938525/can-ios-devices-send-put-requests-to-rails-apps and http://stackoverflow.com/questions/10854419/using-authlogic-with-iphone-client-using-asihttprequest-or-nsurlconnection – iPatel Jun 06 '14 at 10:09

2 Answers2

2

From the error code you're getting:

JSON text did not start with array or object and option to allow fragments not set

it looks like either your request or the server response is not valid JSON.

Are you sure you're posting a valid JSON structure of

{
   "email": "myemail@test.com",
   "password": "mypassword"
}

to the remote host? Are you sure the URL you're posting to is correct? Have you tried testing the url in a tool like Postman?

mcfinnigan
  • 11,442
  • 35
  • 28
  • thank you for response but that said there is working and is there any change for ruby web parsing method is different or what for ios that is my question. – Nitin Gohel Jun 06 '14 at 10:33
0

From the error message it seems you are not getting the Valid JSON,DO ask the ruby developer to check it again also you can use tool mentioned by @mcfinnigan,To check the response you are getting from the web service