7

We've tried numerous times sending update PUT requests to our Rails server and we keep receiving a 404 "The page you were looking for does not exist"

Our update method is very simple (practically generic rails).

def update
@user = User.find(params[:id])

respond_to do |format|
  if @user.update_attributes(params[:user])
    format.html { redirect_to @user, notice: 'User was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

end

When I run CURL from the command line to test the update method, it works fine. But, when I try the same methods in objective-c, it always results in a 404 error.

So my question is: can iOS devices send PUT requests to servers using NSURLConnection or is this not implemented?

UPDATE::

This doesn't update the database

NSString *dataString = @"user[first_name]=bob";
NSData *postBodyData = [NSData dataWithBytes: [dataString UTF8String] length:[dataString length]];
NSString *urlString = [NSString stringWithFormat:@"example.com/users/1.json"];
NSURL *fullURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fullURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[request setHTTPMethod: @"PUT"];
[request setHTTPBody:postBodyData];
self.todoConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
receivedData = [NSMutableData data];

This updates the database

NSString *urlString = [NSString stringWithFormat:@"example.com/users/1.json?user[first_name]=bob"];
NSURL *fullURL = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fullURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];
[request setHTTPMethod: @"PUT"];
self.todoConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
receivedData = [NSMutableData data];
4

3 に答える 3

7

実際に @akshay1188 には実行可能な解決策がありますが、より洗練されたアプローチは、「application/x-www-form-urlencoded」コンテンツ タイプをリクエストに追加することだと思います。

[request setHTTPMethod:@"PUT"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
于 2012-08-05T03:33:07.907 に答える
6

NSMutableURLRequest を作成し、HTTP メソッドを設定できます。

- (void)setHTTPMethod:(NSString *)method

文字列を指定します@"PUT"

このようなリクエストから NSURLConnection を作成する方法は次のとおりです。

NSMutableURLRequest *myMutableRequest[[NSMutableURLRequest alloc] initWithURL:myURL];
[myMutableRequest setHTTPMethod:@"PUT"];
[NSURLConnection connectionWithRequest:myMutableRequest delegate:myDelegate];
于 2012-06-07T19:27:55.427 に答える
4

HTTPメソッドタイプを@"POST"として設定します

そして、リクエスト本文に次のようにパラメータを追加します:@ "PUT" for key @ "_ method"

動作するはずです。

于 2012-07-25T07:01:38.723 に答える