iOS设备可以将PUT请求发送到Rails应用程序吗? [英] Can iOS devices send PUT requests to Rails apps?

查看:90
本文介绍了iOS设备可以将PUT请求发送到Rails应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经尝试了无数次将更新的PUT请求发送到我们的Rails服务器,并且不断收到404您要查找的页面不存在"

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

结束

当我从命令行运行CURL以测试更新方法时,它工作正常.但是,当我在Objective-c中尝试相同的方法时,总是会导致404错误.

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.

所以我的问题是:iOS设备可以使用NSURLConnection将PUT请求发送到服务器吗?

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

更新::

这不会更新数据库

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];

这将更新数据库

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];

推荐答案

您可以创建NSMutableURLRequest并设置HTTP方法:

You can make an NSMutableURLRequest and set the HTTP method:

- (void)setHTTPMethod:(NSString *)method

提供字符串@"PUT".

在这里,您可以根据此类请求创建NSURLConnection.

Here is how you can create an NSURLConnection from such a request.

NSMutableURLRequest *myMutableRequest[[NSMutableURLRequest alloc] initWithURL:myURL];
[myMutableRequest setHTTPMethod:@"PUT"];
[NSURLConnection connectionWithRequest:myMutableRequest delegate:myDelegate];

这篇关于iOS设备可以将PUT请求发送到Rails应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆