11

I've a web service running on server which return data either in XML format or JSON format. I wanted to request a JSON format but using HTTP Post method.

cottontail
  • 10,268
  • 18
  • 50
  • 51
Amit Vaghela
  • 2,970
  • 7
  • 33
  • 43

3 Answers3

30

This is the code which work for JSON post request, TouchJSON Framework is used for parsing the JSON, thanks 'schwa'.

NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", @"preference", @"uid", nil];
NSArray *objects = [NSArray arrayWithObjects:@"accuser", @"accpass", @"abc_region", @"100", nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSURL *theURL = [NSURL URLWithString:@"http://url.com/request.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0f];
[theRequest setHTTPMethod:@"POST"];

[theRequest setValue:@"application/json-rpc" forHTTPHeaderField:@"Content-Type"];
NSString *theBodyString = [[CJSONSerializer serializer] serializeDictionary:theRequestDictionary];
NSLog(@"%@", theBodyString);
NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"%@", theBodyData);
[theRequest setHTTPBody:theBodyData];

NSURLResponse *theResponse = NULL;
NSError *theError = NULL;
NSData *theResponseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&theResponse error:&theError];
NSString *theResponseString = [[[NSString alloc] initWithData:theResponseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(theResponseString);
NSDictionary *theResponseDictionary = [[CJSONDeserializer deserializer] deserialize:theResponseString];
NSLog(@"%@", theResponseDictionary);
NSString *theGreeting = [theResponseDictionary objectForKey:@"greeting"];
[self setValue:theGreeting forKey:@"greeting"];
Amit Vaghela
  • 2,970
  • 7
  • 33
  • 43
3

Not really sure what your question is exactly. But google "TouchJSON" that should help you get started.

schwa
  • 11,962
  • 14
  • 43
  • 54
0

Sorry for errors and memory leaks, but how about something like:

CFURLRef url = CFURLCreateWithString(NULL, CFSTR("http://example.com/post"), NULL);
CFHTTPMessageRef msg = CFHTTPMessageCreateRequest(
    NULL,
    CFSTR("POST"),
    url,
    kCFHTTPVersion1_1);

const char *body = "key=value&id=30293";
CFDataRef bodyData = CFDataCreate(NULL, body, strlen(body));
CFHTTPMessageSetBody(msg, bodyData);

CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(NULL, myRequest);
CFReadStreamOpen(myReadStream);
CFHTTPMessageRef myResponse = CFReadStreamCopyProperty(
    myReadStream,
    kCFStreamPropertyHTTPResponseHeader);

//
// Handle myResponse
//

CFReadStreamClose(myReadStream);
CFRelease(myReadStream);
CFRelease(bodyData);
CFRelease(msg);
CFRelease(url);
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
  • Really carbon-ish solution here -- not going to work very well on the iPhone at all. – Nik Reiman Aug 06 '10 at 12:04
  • That's Core Foundation, not Carbon. CFNetwork (which that is) works just fine on iOS. I wouldn't prefer it to a Cocoa solution, but at first glance I don't see anything wrong with this code. Core Foundation is very useful at times. – Paul Collins Oct 21 '11 at 01:12