I'm trying to post some JSON to a server.
Unfortunately it keeps returning an error. HTTP Status 500 - The server encountered an internal error () that prevented it from fulfilling this request.
The server is newly built so I'm not 100% sure it is working as it should, but I've been assured by the developer that it has been tested.
Can anyone please let me know if I'm missing something crucial here:
This is how I'm generating the request:
- I start with the SBJsonWriter object from the SBJSON library
- Make a dictionary with the params and values I want.
- Convert dict to jsonData with [jsonWriter dataWithObject:dict];
- Set up the NSMutableURLRequest with url
- Add in header fields
- request.
Code:
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSDictionary *dict = [NSDictionary
dictionaryWithObjects:[NSArray arrayWithObjects:@"Mike", @"Baldock", @"email@scet.co", @"qwerty", @"07777777777", nil]
forKeys:[NSArray arrayWithObjects:@"firstname", @"lastname", @"email_address", @"password", @"mobile", nil]];
NSString *jsonString = [jsonWriter stringWithObject:dict];
NSData *jsonData = [jsonWriter dataWithObject:dict];
NSLog(@"JSON String : %@", jsonString);
//Signup
NSURL *url = [NSURL URLWithString:@"http://www.bouncefootball.com/mobile/signup"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: jsonData];
NSLog(@"request : %@", request);
NSLog(@"request headers : %@", [request allHTTPHeaderFields]);
NSLog(@"request body : %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
responseData = [[NSMutableData data] retain];
[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
To look at the data returned I'm printing out in didReceiveData
Code:
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
#ifdef DEBUG
NSLog(@"Did receive Data");
#endif
[responseData appendData:data];
NSString *a = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@", a);
}
I'm wondering if the problem is with my header fields?
This is what my request logs look like:
request : <NSMutableURLRequest http://www.bouncefootball.com/mobile/signup>
request headers : {
Accept = "application/json";
"Content-Length" = 118;
"Content-Type" = "application/json";
}
request body {"firstname":"Mike","password":"qwerty","mobile":" 07777777777","lastname":"Baldock","email_address": "email@scet.co"}
Any help would be really appreciated, before I hassle the guy at the server end!
Cheers.