Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-22-2011, 05:31 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Glasgow
Posts: 58
MBaldo83 is on a distinguished road
Default Posting JSON request

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.
MBaldo83 is offline   Reply With Quote
Old 09-22-2011, 08:49 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

First of all, since it clearly states that the error is happening on the server, maybe the developer can add some logging in to help you pinpoint whether you are not sending the data correctly, or something else is going wrong on the server.
smithdale87 is offline   Reply With Quote
Old 09-22-2011, 09:56 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Location: Glasgow
Posts: 58
MBaldo83 is on a distinguished road
Default

Quote:
Originally Posted by smithdale87 View Post
First of all, since it clearly states that the error is happening on the server, maybe the developer can add some logging in to help you pinpoint whether you are not sending the data correctly, or something else is going wrong on the server.
Yeah, good idea, I've asked him now, I was just wondering if there was a glaringly obvious mistake at my end, could save him some head scratching!

I've parsed plenty of JSON coming in, but not done any POSTing before.

Cheers
MBaldo83 is offline   Reply With Quote
Old 09-22-2011, 06:17 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 97
Bopols is on a distinguished road
Default

save yourself a lot of trouble and use the ASIHTTPREQUEST class ASIHTTPRequest Documentation - All-Seeing Interactive

It's got a lot of helper functions and can do asynchronous calls in just a few lines of code.

Also, you can test posting data to the server by using Firefox Poster add-on. Also installing HTTPFox in firefox will allow you to trace the request and response returned from the server.

Last edited by Bopols; 09-22-2011 at 06:19 PM. Reason: .
Bopols is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
14 members and 378 guests
7twenty7, eski, EvilElf, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0