Created
March 3, 2015 04:05
-
-
Save jungchris/c146c03769bcb3ccd3e5 to your computer and use it in GitHub Desktop.
Sending HTTP POST with JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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