Skip to content

Instantly share code, notes, and snippets.

@nk-gears
Created November 30, 2014 17:16
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 nk-gears/f5d00ed882c781a58ab9 to your computer and use it in GitHub Desktop.
Save nk-gears/f5d00ed882c781a58ab9 to your computer and use it in GitHub Desktop.
POST JSON from OBJ-C
NSString *queryString = [NSString stringWithFormat:@"http://chrisrisner.com/Labs/day7test.php?name=%@", [self.txtName text]];
NSMutableURLRequest *theRequest=[NSMutableURLRequest
requestWithURL:[NSURL URLWithString:
queryString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSDictionary* jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"Value1", @"Key1",
@"Value2", @"Key2",
nil];
NSError *error;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary
options:NSJSONWritingPrettyPrinted error:&error];
[theRequest setHTTPMethod:@"POST"];
[theRequest addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
// should check for and handle errors here but we aren't
[theRequest setHTTPBody:jsonData];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment