Skip to content

Instantly share code, notes, and snippets.

@jeena
Created June 18, 2010 23:31
Show Gist options
  • Save jeena/444361 to your computer and use it in GitHub Desktop.
Save jeena/444361 to your computer and use it in GitHub Desktop.
- (void)postNewEntry {
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:
@"A title", @"title",
@"Some text for body", @"body", nil];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dict
format:NSPropertyListBinaryFormat_v1_0
errorDescription:nil];
NSURL *url = [NSURL URLWithString:@"http://0.0.0.0:3000/posts"];
NSMutableData *postData = [NSMutableData dataWithBytes:[@"post=" UTF8String] length:[@"post=" length]];
[postData appendData:data];
NSLog(@"test");
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
// Init and set fields of the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@"POST"];
[request setURL:url];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
// Return data of the request
NSData *receivedData = [[NSMutableData data] retain];
NSLog(@"%@", receivedData);
[receivedData release];
}
[request release];
[dict release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment