0
votes

I'm trying make a POST to codeIgniter webservice, but i always get the same message:

-> [13884:a0b] String sent from server Disallowed Key Characters.

any ideia? thks

define kUrl @"http://gf.kineth.com/index.php/json_api/"

This is my function

-(void)postMessageLogin:(NSString *)login:(NSString *)pass{//Login device selected = 1;

NSMutableString *postString = [NSMutableString stringWithString:kUrl];
//a = 1 melhor variavel do mundo, faz cenas incriveis




[postString appendFormat:@"loginUser?data={\"email\":\"[email protected]\",\"password\":\"123456\"}"];


[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];



NSLog(@"%@",postString);

NSString *myRequestString = postString;
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];

request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:kUrl ] ];
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];



NSURLConnection * connection = [[NSURLConnection alloc]
                                initWithRequest:request
                                delegate:self startImmediately:NO];


[connection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                      forMode:NSDefaultRunLoopMode];
[connection start];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{ NSLog(@"String sent from server %@",[[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding]);

     NSError *error;
    NSDictionary *dic;
    dic = [NSJSONSerialization JSONObjectWithData:theData options:0 error:&error];



    NSLog(@"[SL] JsonPost : didReceiveData %@",dic);
    NSLog(@"[SL] JsonPost : didReceiveData %@",error);


[connection cancel];

}

2

2 Answers

0
votes

_ character is not allowed in URI

config/config.php

$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-()';

0
votes

I was having this same issue sending json post data, and found it was due to me not specifying the content-type in the http request.

I added this parameter to my URLRequest() and the issue was solved, sorry I can not answer in ObC as well, but hope this helps someone in the future

swift3

request.addValue("application/json", forHTTPHeaderField: "Content-Type")