Hi Everyone,
I’m currently completing a project at university, where I am designing an iPhone application that is using the Zabbix API. However I am having trouble communicating to the Zabbix server using the JSON RPC.
- This is a lengthy post, but only has 3 Questions, please read on
I’ve managed to find out how to do the following:
1. Convert objective C objects into JSON language;
using SBJSON framework
2. Package the parsed JSON line of code and compose a HTTP Post request to the API url
following this tutorial
However when I did a print out of the JSON code after step 1, I noticed that the ID field contains “ “ around the number. Which is not the convention used by the documentation. (see the question bellow)
Q1: Could this be the reason why my application receives no data back from the API? Also do I need an empty “auth” field to be sent?
JSON Print out
{
"method":"user.login",
"id":"1",
"jsonrpc":"2.0",
"params":{
"password":"zabbix",
"user":"Admin"
}
}
Zabbix Documentation
{
"jsonrpc": "2.0",
"method": "user.authenticate",
"params": {
"user": "Admin",
"password": "zabbix"
},
"auth": null,
"id": 0
}
Q2: Is there any additional code needed for the HTTP Post request?
- (void)authenticateZabbix {
NSString *ID_noFormat = [[NSString alloc ] initWithString:@"1"];
// NSString *blank_noFormat = [[NSString alloc ] initWithString:NULL];
NSMutableDictionary *jsonObject = [NSMutableDictionary dictionary];
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[jsonObject setObject:@"2.0" forKey:@"jsonrpc"];
[jsonObject setObject:@"user.login" forKey:@"method"];
[params setObject:@"Admin" forKey:@"user"];
[params setObject:@"zabbix" forKey:@"password"];
[jsonObject setObject
arams forKey:@"params"];
[jsonObject setObject:ID_noFormat forKey:@"id"];
// [jsonObject setObject:blank_noFormat forKey:@"auth"];
// Raw Format
NSString* jsonString = jsonObject.JSONRepresentation;
// jsonString now contains your example strings.
debug(jsonString);
// 2. Change Payload into NSData
NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
// Calculate the length of code
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
// 3. Create URLRequest object and initialize it.
// Initiate connection
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://10.0.0.3/api_jsonrpc.php"]];
[request setHTTPMethod:@"POST"];
[request setValue
ostLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
// Set the HTTPBody of the urlrequest with postData.
[request setHTTPBody
ostData];
// 4. Now, create URLConnection object. Initialize it with the URLRequest.
NSURLConnection *connection2 = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 5. Check that the connection is Valid
if (connection2) {
NSLog(@"Connection Successful");}
else{
NSLog(@"Connection failed");
}
[connection2 release];
}
When I run the code the Connection is Successful.
Using the NSLog statements, I tracing through the application. The app returns back on the “connectionDidFinishLoading” method not the “connection didReceiveData”.
The best thing is the connection did not return on the fail method.
Q3: So I think the problem is the quoted ID field. If I managed to take away the quotes it should work? Can anyone else see anything wrong?
Thanks for reading,
Paperback writer
p.s all the smiley faces equal : <colon> and letter P
I’m currently completing a project at university, where I am designing an iPhone application that is using the Zabbix API. However I am having trouble communicating to the Zabbix server using the JSON RPC.
- This is a lengthy post, but only has 3 Questions, please read on
I’ve managed to find out how to do the following:
1. Convert objective C objects into JSON language;
using SBJSON framework
2. Package the parsed JSON line of code and compose a HTTP Post request to the API url
following this tutorial
However when I did a print out of the JSON code after step 1, I noticed that the ID field contains “ “ around the number. Which is not the convention used by the documentation. (see the question bellow)
Q1: Could this be the reason why my application receives no data back from the API? Also do I need an empty “auth” field to be sent?
JSON Print out
{
"method":"user.login",
"id":"1",
"jsonrpc":"2.0",
"params":{
"password":"zabbix",
"user":"Admin"
}
}
Zabbix Documentation
{
"jsonrpc": "2.0",
"method": "user.authenticate",
"params": {
"user": "Admin",
"password": "zabbix"
},
"auth": null,
"id": 0
}
Q2: Is there any additional code needed for the HTTP Post request?
- (void)authenticateZabbix {
NSString *ID_noFormat = [[NSString alloc ] initWithString:@"1"];
// NSString *blank_noFormat = [[NSString alloc ] initWithString:NULL];
NSMutableDictionary *jsonObject = [NSMutableDictionary dictionary];
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[jsonObject setObject:@"2.0" forKey:@"jsonrpc"];
[jsonObject setObject:@"user.login" forKey:@"method"];
[params setObject:@"Admin" forKey:@"user"];
[params setObject:@"zabbix" forKey:@"password"];
[jsonObject setObject
arams forKey:@"params"];[jsonObject setObject:ID_noFormat forKey:@"id"];
// [jsonObject setObject:blank_noFormat forKey:@"auth"];
// Raw Format
NSString* jsonString = jsonObject.JSONRepresentation;
// jsonString now contains your example strings.
debug(jsonString);
// 2. Change Payload into NSData
NSData *postData = [jsonString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
// Calculate the length of code
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
// 3. Create URLRequest object and initialize it.
// Initiate connection
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://10.0.0.3/api_jsonrpc.php"]];
[request setHTTPMethod:@"POST"];
[request setValue
ostLength forHTTPHeaderField:@"Content-Length"];[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
// Set the HTTPBody of the urlrequest with postData.
[request setHTTPBody
ostData];// 4. Now, create URLConnection object. Initialize it with the URLRequest.
NSURLConnection *connection2 = [[NSURLConnection alloc] initWithRequest:request delegate:self];
// 5. Check that the connection is Valid
if (connection2) {
NSLog(@"Connection Successful");}
else{
NSLog(@"Connection failed");
}
[connection2 release];
}
When I run the code the Connection is Successful.
Using the NSLog statements, I tracing through the application. The app returns back on the “connectionDidFinishLoading” method not the “connection didReceiveData”.
The best thing is the connection did not return on the fail method.
Q3: So I think the problem is the quoted ID field. If I managed to take away the quotes it should work? Can anyone else see anything wrong?
Thanks for reading,
Paperback writer
p.s all the smiley faces equal : <colon> and letter P