Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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