Take the 2-minute tour ×

I have the following code:

NSString *post = @"i hope this gets there";

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];

NSURL *url = [NSURL URLWithString:@"http://www.oneoftwosites.com"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];

[NSURLConnection connectionWithRequest:request delegate:nil];

//potential response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//convert response so that it can be LOG'd
NSString *returnString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];

NSLog(@"RETURN: %@", returnString);

I have tested it with 2 different websites: http://www.htmlcodetutorial.com/cgi-bin/mycgi.pl and http://instagraph.me/post, by substituting those urls in for http://www.oneoftwosites.com. For some reason, it only works with the first website (which is written in perl), and not with the second (which is written in .php).

Does anyone have any ideas why this might be? It seems dumb that it would only work sometimes, but I don't really have any idea why it doesn't work for the second website (I haven't seen the code but I did see some sample output).

EDIT: I added

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; and tried a couple different types but still no luck

share|improve this question
You might try setting the HTTP Host header? That has been a problem I have had in the past. –  Brian Walker Aug 15 '12 at 21:41
add comment

2 Answers

up vote 1 down vote accepted

What kind of POST data does Instagraph expect? Chances are, it's some kind of structured data, like a URL encoded form, or XML, or JSON. Your string is neither of those. So provide a Content-Type, and format your POST data accordingly. Maybe you'll get better luck then.

EDIT: Your second site is Instagraph. It's a public website. It probably has a documented API - what fields and values should there be in a POST. Is the POST data that you're sending up to that spec? So far, I don't think so. If you sending dummy, test data - why are you expecting it to work? What sensible effect are you expecting?

The line "i hope this gets there" does not, by the way, constitute a valid data block of type application/x-www-form-urlencoded.

share|improve this answer
thanks i will try that ! –  BloonsTowerDefence Aug 15 '12 at 21:41
add comment

The post length should come from postData

share|improve this answer
add comment

Your Answer


