RESTなリソースへリクエストを送りたい

NSMutableRequest#setHTTPMethod:でGET/POST/PUT/DELETEを指定することでRESTなリソースに対してのリクエストを送信することができる。

POSTとPUTの時には送信するコンテンツのContent-Typeを指定することを忘れずに。(今回はXMLデータを送信するのでapplication/xmlを指定している)

// POST
{
	NSURL *url = [NSURL URLWithString:@"http://example.com/users.xml"];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
	[request setValue:@"application/xml"forHTTPHeaderField:@"Content-Type"];
	[request setHTTPMethod:@"POST"];	
	NSString *content = @"<user><name>post</name><age>55</age></user>";
	[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];
	[NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]];
}

// PUT
{
	NSURL *url = [NSURL URLWithString:@"http://example.com/users/1.xml"];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
	[request setValue:@"application/xml"forHTTPHeaderField:@"Content-Type"];
	[request setHTTPMethod:@"PUT"];	
	NSString *content = @"<user><name>HOGE</name><age>99</age></user>";
	[request setHTTPBody:[content dataUsingEncoding:NSUTF8StringEncoding]];
	[NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]];
}

// GET
{
	NSURL *url = [NSURL URLWithString:@"http://example.com/users.xml"];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
	[request setHTTPMethod:@"GET"];	
	[NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]];
}

// DELETE
{
	NSURL *url = [NSURL URLWithString:@"http://example.com/users/10.xml"];
	NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
	[request setHTTPMethod:@"DELETE"];
	[NSURLConnection connectionWithRequest:request delegate:[[[ConnectionHandler alloc] init] autorelease]];
}

あと、受け取ったレスポンスを表示するためのNSURLConnectionのdelegateクラスが地味に便利なので、おまけとしてここに載せておくよ。

// ConnectionHandler.h
#import <Foundation/Foundation.h>
@interface ConnectionHandler : NSObject {
	NSMutableData *receivedData;
	NSStringEncoding receivedDataEncoding;
}
@end

// ConnectionHandler.m
#import "ConnectionHandler.h"

@implementation ConnectionHandler

- (id)init {
	self = [super init];
	if (self) {
		receivedData = [[NSMutableData alloc] init];
	}
	return self;
}

// サーバからレスポンスヘッダを受け取ったときに呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
	// 送信されるデータの文字コードを取得
	NSString *encodingName = [response textEncodingName];
	
	NSLog(@"受信文字コード: %@", encodingName);
	
	if ([encodingName isEqualToString: @"euc-jp"]) {
		receivedDataEncoding = NSJapaneseEUCStringEncoding;
	} else {
		receivedDataEncoding = NSUTF8StringEncoding;
	}
}

// サーバからデータを受け取るたびに呼び出される
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	// 取得したデータをreceivedDataへ格納する
	NSLog(@"受信データ(バイト数): %d", [data length]);
	[receivedData appendData:data];
}

// データの取得が終了したときに呼び出される
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
	NSString *result = [[NSString alloc] initWithData:receivedData encoding:receivedDataEncoding];
	NSLog(@"データの受信完了: %@", result);
	[result release];
	[receivedData release];
}

@end