Iphone – ASIHttpRequest POST

asihttprequestiphone

I am working on an iphone app and my requirements are to make a simple post to a web service and the web service returns a json response.

The sample code they give for a post is this, but how do I get a response? I see there is asynchronous code for doing a get, but how do I do it async for a post?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

Best Answer

If you're targeting iOS 4+ you can use blocks, which makes asynchronous code a little bit nicer. You won't have to deal with delegates.

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

[request setCompletionBlock:^{
    NSLog(@"%@", request.responseString);
}];

[request setFailedBlock:^{

    NSLog(@"%@", request.error);
}];

[request startAsynchronous];    

More about blocks http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

Related Topic