Skip to content

Instantly share code, notes, and snippets.

@jungchris
Created March 3, 2015 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jungchris/c146c03769bcb3ccd3e5 to your computer and use it in GitHub Desktop.
Save jungchris/c146c03769bcb3ccd3e5 to your computer and use it in GitHub Desktop.
Sending HTTP POST with JSON
- (void)postJSONToSlack {
NSLog(@"Post JSON to Slack");
// BuiltInNM #random
// NSMutableURLRequest *request = [NSMutableURLRequest
// requestWithURL:[NSURL URLWithString:@"https://hooks.slack.com/services/"]];
// This is for NMDevs to #random
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:@"https://hooks.slack.com/services/"]];
NSDictionary *requestData = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Test post to NMDevs from iOS.\nFun with Slack integrations.", @"text",
nil];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:requestData options:0 error:&error];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
NSLog(@"Connection Successful");
} else {
NSLog(@"Connection could not be made");
// todo: manually load content as a backup plan here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment